Aggregate support for QuerySets
|Reported by:||Owned by:||Nicolas Lara|
|Component:||Database layer (models, ORM)||Version:||queryset-refactor|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
Add aggregate support for QuerySets according to what have been discussed in http://groups.google.com/group/django-developers/browse_thread/thread/3d4039161f6d2f5e/302b088cc816c0a6?hl=en#302b088cc816c0a6
The proposed syntax includes the two QuerySet modifiers _annotate_ and _aggregate_ to augment the objects with aggregate information and retrieve aggregate information regarding the whole QuerySet respectively.
The notation for defining the fields and aggregate functions to use would be done in filter-like syntax
with the possibility of specifying aggregates in object form for non aliased aggregates (only the simple form is intended, for a more complex approach to object aggregates: http://code.djangoproject.com/ticket/6811 ). In this case a standard alias should be generated (i.e. quantity_sum, payment_avg)
The GROUP BY is to be done internally. To specify the fields to be grouped it could be done with values()
No way to specify that only the aggregated fields should appear in the results is intended because this could be done with the modifiers proposed in #5420.
Ordering should be specified using order_by standard syntax with the aliased name.
As extra things that could be implemented are:
- Custom aggregate creation (possibly as follows)
>>> my_own_aggregate = Aggregate( sum('field_1') * 'field2', 'default_name') >>> queryset.aggregate(purchases__price__custom=my_own_aggregate('alias'))
- F syntax for refering to other fields in the model.
Please refer to the discussion thread for a more in depth description
Change History (4)
comment:1 Changed 9 years ago by
|Owner:||changed from nobody to anonymous|
|Patch needs improvement:||set|
|Status:||new → assigned|
comment:2 Changed 9 years ago by
|Owner:||changed from anonymous to Nicolas Lara|
|Status:||assigned → new|