title: Django Inline for ManyToMany generate duplicate queries
body: <p>I'm experiencing some major performing issue with my django admin. Lots of duplicate queries based on how many inlines that I have.</p>
  
  <p>models.py</p>
  
  <pre><code>class Setting(models.Model):
      name = models.CharField(max_length=50, unique=True)
  
      class Meta:
          ordering = ('name',)
  
      def __str__(self):
          return self.name
  
  
  class DisplayedGroup(models.Model):
      name = models.CharField(max_length=30, unique=True)
      position = models.PositiveSmallIntegerField(default=100)
  
      class Meta:
          ordering = ('priority',)
  
      def __str__(self):
          return self.name
  
  
  class Machine(models.Model):
      name = models.CharField(max_length=20, unique=True)
      settings = models.ManyToManyField(
          Setting, through='Arrangement', blank=True
      )
  
      class Meta:
          ordering = ('name',)
  
      def __str__(self):
          return self.name
  
  
  class Arrangement(models.Model):
      machine = models.ForeignKey(Machine, on_delete=models.CASCADE)
      setting = models.ForeignKey(Setting, on_delete=models.CASCADE)
      displayed_group = models.ForeignKey(
          DisplayedGroup, on_delete=models.PROTECT,
          default=1)
      priority = models.PositiveSmallIntegerField(
          default=100,
          help_text='Smallest number will be displayed first'
      )
  
      class Meta:
          ordering = ('priority',)
          unique_together = (("machine", "setting"),)
  </code></pre>
  
  <p>admin.py</p>
  
  <pre><code>class ArrangementInline(admin.TabularInline):
      model = Arrangement
      extra = 1
  
  
  class MachineAdmin(admin.ModelAdmin):
      inlines = (ArrangementInline,)
  </code></pre>
  
  <p>If I have 3 settings added on inline form and 1 extra, I have about 10 duplicate queries</p>
  
  <pre><code>SELECT "corps_setting"."id", "corps_setting"."name", "corps_setting"."user_id", "corps_setting"."tagged", "corps_setting"."created", "corps_setting"."modified" FROM "corps_setting" ORDER BY "corps_setting"."name" ASC
  - Duplicated 5 times
  
  SELECT "corps_displayedgroup"."id", "corps_displayedgroup"."name", "corps_displayedgroup"."color", "corps_displayedgroup"."priority", "corps_displayedgroup"."created", "corps_displayedgroup"."modified" FROM "corps_displayedgroup" ORDER BY "corps_displayedgroup"."priority" ASC
  - Duplicated 5 times.
  </code></pre>
  
  <p>Could someone please tell me what I'm doing wrong right here? I've spent 3 days trying to figure the problem out myself without luck.</p>
  
  <p>The issue gets worse when I have about 50 settings inlines of a Machine, I will have ~100 queries.</p>
  
  <p><a href="https://i.stack.imgur.com/5XFX2.png" rel="noreferrer">Here is the screenshot</a></p>