Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
django 1.8 - QuerySet generated include columns not in values

I am trying to get a queryset displaying the average rate of comments. The query looks pretty simple - although Django seems to add automatically an extra field created_at in the generated group by statement. Is there any way to remove it from the group by clause ? Is it there because of the default order by defined in the META class of the models ?

GROUP BY "authentification_antenne"."name", "review"."created_at" instead of just GROUP BY "authentification_antenne"."name"

Review.objects.select_related(
'appartement__antenne').values('appartement__antenne__name').annotate(average_rating=Avg('rating')).query

SELECT 
"authentification_antenne"."name", AVG("review"."rating") AS "average_rating" FROM "review" 
LEFT OUTER JOIN "staffing_appartement" ON ( "review"."appartement_id" = "staffing_appartement"."numero_contrat" ) 
LEFT OUTER JOIN "authentification_antenne" ON ( "staffing_appartement"."antenne_id" = "authentification_antenne"."id" ) 
GROUP BY "authentification_antenne"."name", "review"."created_at" 
ORDER BY "review"."created_at" DESC

And the models.py

 class Review(models.Model):
     guest = models.ForeignKey(Guest)
     comment = models.TextField()
     rating = models.IntegerField(null=True)
     created_at = models.DateTimeField()
     imported_at = models.DateTimeField(auto_now=True)
     appartement = models.ForeignKey('staffing.appartement', null=True)

     class Meta:
         ordering = ['-created_at']

Edit - I confirm the statement generated is correct when the created_at ordering is removed from the models.

Edit 2 - I resolved this by adding my values in an order_by clause to remove the default filtering

AirbnbReview.objects.select_related('appartement__antenne').values('appartement__antenne__name').order_by('appartement__antenne__name').annotate(average_rating=Avg('rating'))

Thank you

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!