title: python django prefetch_related on form set and inlineformset_factory
body: <p>inlineformset_factory` with 50 extra rows.
  It is taking forever to load and getting 204 queries are running to get that data with almost 200 duplicate queries</p>
  <p>Here are my models</p>
  <pre><code>class Item_type(models.Model):
      title = models.CharField(max_length=100, null=True)
      alias = models.CharField(max_length=20, null=True, blank=True)
  
      def __str__(self):
          return str(self.id) + &quot; (&quot;+ str(self.title) + &quot;)&quot;
  
  
  class Item(models.Model):
      name = models.CharField(max_length=150, null=True, unique=True)
      active = models.BooleanField(default=False)
      code = models.CharField(max_length=20, null=True, blank=True)
      item_class = models.ForeignKey(Item_class,null=True, blank=True, on_delete=models.CASCADE, related_name='item_class')
      item_type = models.ForeignKey(Item_type,null=True, blank=True, on_delete=models.CASCADE, related_name='item_type')
      alias = models.CharField(max_length=20, null=True, blank=True)
      barcode = models.CharField(max_length=50, null=True, blank=True)
      stuff_qty = models.IntegerField(null=True, blank=True)
  
      def __str__(self):
          return str(self.alias) + &quot; &quot; + str(self.name + &quot; (&quot; + str(self.id) + &quot;)&quot;)
  
  
  class ReceipeMaster(models.Model):
      TYPE_OPTIONS = (('FINISHED GOODS', 'FINISHED GOODS'),)
      receipe_type = models.CharField(max_length=50, choices=TYPE_OPTIONS, default=TYPE_OPTIONS[0])
      item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='receipe')
      units = models.IntegerField(default=1)
      active = models.BooleanField(default=True)
  
      def __str__(self):
          return str(self.item.name)
  
      def get_absolute_url(self):
          return reverse(&quot;item_management:recipes_show&quot;, kwargs={&quot;pk&quot;: self.pk})
  
  
  class ReceipeDetail(models.Model):
      master = models.ForeignKey(ReceipeMaster, on_delete=models.CASCADE, related_name='items')
      item_type = models.ForeignKey(Item_type, null=True, on_delete=models.PROTECT)
      item = models.ForeignKey(Item, on_delete=models.PROTECT)
      quantity = models.IntegerField()
  
      def __str__(self):
          return str(self.item.name) + &quot; (&quot; + str(self.master) + &quot;)&quot;
  </code></pre>
  <p>how do I get prefetch_related in the <code>forms.inlineformset_factory</code></p>
  <p>here is the code from forms.py</p>
  <pre><code>AddReceipeDetailFormSet = forms.inlineformset_factory(
      ReceipeMaster,
      ReceipeDetail,
      fields=( 'item_type', 'item', 'quantity'),
      extra=50,
      widgets={
          'item_type': forms.Select(attrs={'class': 'form-control'}),
          'item': forms.Select(attrs={'class': 'form-control'}),
          'quantity': forms.NumberInput(attrs={'class': 'form-control'}),
      }
  )
  </code></pre>
  <p>Here is my view from views.py</p>
  <pre><code>def create_receipe(request):
      child_forms = AddReceipeDetailFormSet(request.POST or None)
      parent_form = ReceipeCreateForm(request.POST or None)
      context = {'parent_form': parent_form, 'child_forms': child_forms}
      if request.method == 'POST':
          if parent_form.is_valid():
              print('parent form is valid')
          else:
              print('invalid parent form', parent_form.errors)
          if child_forms.is_valid():
              print('child form is valid')
          else:
              print('invalid child form', child_forms.errors)
          print(&quot;post request is received***&quot;, request.POST)
      return render(request, 'receipes/create.html', context=context)
  </code></pre>
  <p>regards</p>