Django 1.8 - Created QuerySet includes columns not in values
I am trying to get a query displaying the average comment rate. The request looks pretty straightforward - although Django seems to automatically add an extra created_at field in the generated expression group. Is there a way to remove it from the article group? Is it because of the default order 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 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 that the generated statement is correct when the created_at order is removed from the models.
Edit 2 - I solved it by adding my values ββto the 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
source to share
No one has answered this question yet
Check out similar questions: