django rest framework - backward serialization to avoid prefetch_related : <p>I have two models, <code>Item</code> and <code>ItemGroup</code>:</p>

<pre><code>class ItemGroup(models.Model):
   group_name = models.CharField(max_length=50)
   # fields..

class Item(models.Model):
   item_name = models.CharField(max_length=50)
   item_group = models.ForeignKey(ItemGroup, on_delete=models.CASCADE)
   # other fields..
</code></pre>

<p>I want to write a serializer that will fetch all item groups with their item list as a nested array.</p>

<p>So I want this output:</p>

<pre><code>[ {group_name: "item group name", "items": [... list of items ..] }, ... ]
</code></pre>

<p>As I see, I should write this with django rest framework:</p>

<pre><code>class ItemGroupSerializer(serializers.ModelSerializer):
   class Meta:
      model = ItemGroup
      fields = ('item_set', 'group_name') 
</code></pre>

<p>Means, I have to write a serializer for <code>ItemGroup</code> (not for <code>Item</code>).
To avoid many queries I pass this queryset:</p>

<pre><code>ItemGroup.objects.filter(**filters).prefetch_related('item_set')
</code></pre>

<p>The problem that I see is, for a large dataset, <code>prefetch_related</code> results in an extra query with a VERY large sql <code>IN</code> clause, which I could avoid with the query on the Item objects instead:</p>

<pre><code>Item.objects.filter(**filters).select_related('item_group')
</code></pre>

<p>Which results in a JOIN which is way better.</p>

<p>Is it possible to query <code>Item</code> instead of <code>ItemGroup</code>, and yet to have the same serialization output? </p>
