Why the Django-ORM doesnt have GroupBy QuerySet method?

I’m know that we have aggregation and annotate, but what if I have for eaxmple a Student model with ManyToMany field to Exam model , and I want to review all of its grade in a list.

Currently we will do:
Student.objects.values(‘id’, ‘name’, ‘exams__grade’)
and will get:

[{ “first_name”, “first_grade”}, {“first_name”, “second_grade”}, … etc… ]

Can we do currently a query using the Django-ORM that will return the below example ?
[{ "first_name", ["first_grade", "second_grade"]}, ... etc... ]

Yes if you’re on Postgres you can use ArrayAgg for that

Student.objects.values("name").annotate(
    grades=ArrayAgg("exams__grade"),
)

Otherwise you’ll have to create your own flavour of JSON_ARRAYAGG based on your backends.

I do use Postgres, Thanks!.

just for my personal knowledge, is there any structural issue that limit ORMs from implement the group-by caluse ? or something alike?

There’s no limitation or structural issue at play here, not exposing GROUP BY through a group_by method but through values(*group_by) is by design.

Groups are useful for two purposes, aggregation and avoiding duplicates.

In the first case Django does it implicitly the moment an aggregation function is annotated or aggregation is performed through aggregate and allows for values to be used to specify the group explicitly.

In the second one, where multi-valued relationships are spanned but no aggregation is used and duplicates are not desirable, distinct(*on) can be used which usually results in the exact same plan.

1 Like

Ok, thanks!
(sorry for the delay :slight_smile: