title: Django making tons of duplicate queries
body: <p>Im working with on an ecommerce project where django is making tons of tons of duplicate queries while fetching products from database using django ORM.</p>
  <p>please find the below codes for your reference help to resolve.</p>
  <p>Thanks in advance.</p>
  <p><strong>Product model</strong></p>
  <pre><code>class Product(models.Model):
  variations = (
      ('None', 'None'),
      ('Size', 'Size'),
  )
  name = models.CharField(max_length=200, unique=True)
  store = models.ManyToManyField(Store)
  slug = models.SlugField(null=True, blank=True, unique=True, max_length=500)
  sku = models.CharField(max_length=30, null=True)
  tax = models.IntegerField(null=True, blank=True)
  stock = models.CharField(max_length=10, null=True)
  variations = models.CharField(choices=variations, max_length=20)
  short_description = models.CharField(max_length=500, null=True)
  details = RichTextUploadingField(null=True, blank=True)
  price = models.DecimalField(max_digits=10, decimal_places=2)
  discounted_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, 
  blank=True)
  delivery_time = models.CharField(max_length=5, null=True, blank=True, 
   verbose_name=&quot;Delivery Time&quot;)
  returnable = models.BooleanField(max_length=5, null=True, blank=True, 
  verbose_name=&quot;Returnable&quot;)
  emi = models.BooleanField(max_length=5, null=True, blank=True, verbose_name=&quot;EMI 
  available&quot;)
  image = models.ImageField(upload_to='product/images', default='product.png', 
  null=True, blank=True)
  image_one = models.ImageField(upload_to='product/images', null=True, blank=True)
  image_two = models.ImageField(upload_to='product/images', null=True, blank=True)
  image_three = models.ImageField(upload_to='product/images', null=True, blank=True)
  image_four = models.ImageField(upload_to='product/images', null=True, blank=True)
  image_five = models.ImageField(upload_to='product/images', null=True, blank=True)
  tags = models.ManyToManyField(Tags)
  category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, 
  blank=True,
                               related_name='products')
  status = models.CharField(max_length=20, choices=(('Active', 'Active'), ('Inactive', 
  'Inactive')))
  brand = models.ForeignKey(Brand, on_delete=models.PROTECT, blank=True, null=True)
  offer = models.ForeignKey(Offer, on_delete=models.CASCADE, null=True,
                            blank=True)  # This is used only for filtration
  color = models.ManyToManyField(Colors)
  
  def save(self, *args, **kwargs):
      if not self.slug:
          self.slug = slugify(self.name)
      super(Product, self).save(*args, **kwargs)
  
  def __str__(self):
      return str(self.pk) + &quot;:&quot; + self.name + &quot;:&quot;
  
  @property
  def get_rating(self):
      total = sum(int(review['stars']) for review in 
      self.reviews.select_related('r_product').values())
      return total
  </code></pre>
  <p><strong>Product review class</strong></p>
  <pre><code>class ProductReview(models.Model):
  r_product = models.ForeignKey(Product, on_delete=models.CASCADE, 
  related_name='reviews')
  review_by = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True, 
  related_name='reviews')
  stars = models.IntegerField(default=0,
                              validators=[MaxValueValidator(5), MinValueValidator(0)]
                              )
  notes = models.CharField(max_length=500, null=True, blank=True)
  date = models.DateField(auto_now_add=True)
  </code></pre>
  <p><strong>This is django debug toolbar result</strong></p>
  <pre><code>   SELECT &quot;backend_productreview&quot;.&quot;id&quot;,
     &quot;backend_productreview&quot;.&quot;r_product_id&quot;,
     &quot;backend_productreview&quot;.&quot;review_by_id&quot;,
     &quot;backend_productreview&quot;.&quot;stars&quot;,
     &quot;backend_productreview&quot;.&quot;notes&quot;,
     &quot;backend_productreview&quot;.&quot;date&quot;
    FROM &quot;backend_productreview&quot;
    WHERE &quot;backend_productreview&quot;.&quot;r_product_id&quot; = '102' 
  </code></pre>
  <p>114 similar queries.  Duplicated 2 times.</p>
  <p><strong>This is query highlighted by django debug toolbar</strong></p>
  <pre><code>total = sum(int(review['stars']) for review in 
  self.reviews.select_related('r_product').values())
  </code></pre>