Opened 17 years ago
Closed 17 years ago
#6821 closed (duplicate)
Aggregate support for QuerySets
Reported by: | Owned by: | Nicolas Lara | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | queryset-refactor |
Severity: | Keywords: | aggregation | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
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
Purchase.objects.all().aggregate(quantity__sum='total_adquisitions', payment__avg='mean_pay')
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)
Purchase.objects.all().aggregate(Sum('quantity'), Avg('payment'))
The GROUP BY is to be done internally. To specify the fields to be grouped it could be done with values()
Purchase.objects.all().values('identifier').aggregate(quantity__sum='num_adquisiitions')
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.
==Extra ==
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
Attachments (1)
Change History (4)
by , 17 years ago
Attachment: | aggregate.diff added |
---|
comment:1 by , 17 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
Added proof-of-concept patch that implements aggregate modifier. (No Joins/Aliases ...yet)
comment:2 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:3 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Duplicate of #3566. You don't need to open a new ticket to work on aggregates - please continue discussions on the original ticket.
aggregate modifier 0.1