Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda