Django: How to prefetch related for a model instance. Perhaps by wrapping in a queryset?
问题 I use Django rest framework and I have decent nesting in my model relations. I'm working on optimizing my queries. Many of my functions consume or manipulate a single instance of model and it's often further downstream in the data flow that it turns out I need some prefetching. One classic instance of this is with DRF serializers. Here's an example. @api_view(['GET']) def fetch_user_profile(request): profile = request.user.profile # has many nested relationships return Response