Add a way to have an aggregate() result as a queryset
|Reported by:||mtredinnick||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If one is trying to construct a complex queryset, involving nesting other querysets, there's currently no way to include the results of an aggregate() call. For example, consider
class Item(models.Model): name = models.CharField(max_length=10)
If I want to include the number of Items in some other queryset, such as
We can't write item_count (which would be Items.objects.aggregate(Count("id"))) as an inner queryset here, since the aggregate() call returns a dictionary, not a queryset.
The return value of aggregate() is fine, but this ticket is about adding a version that does return a queryset. Right now, there's no way to fake this with annotate(...).values(...) since that always introduces a GROUP BY clause and the whole point is not to group by anything.
Not sure whether adding a parameter to aggregate() is the right idea, or adding something to the annotate() route to specify "no grouping whatsoever". This is all 1.2 timeframe stuff, but it's something to think about.
Change History (7)
comment:1 Changed 7 years ago by jacob
- milestone set to 1.2
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted