title: Django: How to prefetch related for a model instance. Perhaps by wrapping in a queryset?
body: <p>I use Django rest framework and I have decent nesting in my model relations.</p>
  
  <p>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.</p>
  
  <pre><code>@api_view(['GET'])
  def fetch_user_profile(request):
      profile = request.user.profile # has many nested relationships
  
      return Response(UserProfileSerializer(profile).data, status=status.HTTP_200_OK) # this ends up taking many queries
  </code></pre>
  
  <p>I've seen some solutions suggesting passing queryset with <code>prefetch_related</code> in the context although I haven't been able to get a full picture on how that would work (I've only found a couple of places that discuss it partially, might open a whole other question on that).</p>
  
  <p><strong>An easy fix (and one that would generalize beyond serializers) would be if I can have a wrapper function to wrap my instance in a queryset and then call prefetch on that, passing it into the serializer.</strong></p>
  
  <p>Something like:</p>
  
  <pre><code>def queryset_wrap(instance):
      return type(instance).objects.filter(id=instance.id)
  </code></pre>
  
  <p><strong>I'm wondering if there's a better way to do this.</strong></p>