I'm using Django 1.6 with PostgreSQL and have following model:
# models.py
class Game(AbstractContentModel, AbstractScoreModel):
name = models.CharField(_("name"), max_length=100, blank=True)
developer = models.CharField(_('Developer'), max_length=255)
distributor = models.CharField(_('Distributor'), max_length=255, blank=True)
# ...
reviews = models.ManyToManyField(Review, related_name="games", blank=True, verbose_name=_("Reviews"))
videos = models.ManyToManyField(Video, related_name="games", blank=True, verbose_name=_("Videos"))
images = models.ManyToManyField(Image, related_name="games", blank=True, verbose_name=_("Gallery"))
I'm trying to get all games and to each of them add count of related videos, reviews and images as follows:
# views.py
qs = Game.objects.all()
qs = qs.annotate(video_count=models.Count('videos'))
qs = qs.annotate(review_count=models.Count('reviews'))
qs = qs.annotate(image_count=models.Count('images'))
Result query is:
SELECT
"content_game"."id",
"content_game"."name",
"content_game"."developer",
"content_game"."distributor",
COUNT("content_game_videos"."video_id") AS "video_count",
COUNT("content_game_reviews"."review_id") AS "review_count",
COUNT("content_game_images"."image_id") AS "image_count"
FROM "content_game"
LEFT OUTER JOIN "content_game_videos" ON ( "content_game"."id" = "content_game_videos"."game_id" )
LEFT OUTER JOIN "content_game_reviews" ON ( "content_game"."id" = "content_game_reviews"."game_id" )
LEFT OUTER JOIN "content_game_images" ON ( "content_game"."id" = "content_game_images"."game_id" )
GROUP BY
"content_game"."id",
"content_game"."name",
"content_game"."developer",
"content_game"."distributor";
My question is - why there are all selected fields in GROUP BY clause? And most importantly, how can I get rid of them besides raw SQL?
I gathered that it would be good to use .values()
, but I want every field in result query. If I just use GROUP BY "content_game"."id"
, the result is the same, but I don't know how to use it like this with Django ORM.