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:
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.