Opened 16 years ago

Closed 13 years ago

Last modified 12 years ago

#10972 closed New feature (duplicate)

Use Expressions with Annotations

Reported by: me@… Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: expression aggregation aggregates annotation
Cc: samt@…, django@…, Alexander Koshelev, miracle2k, meticulos_slacker, bgrand, nwp, Dan Fairs Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

It would be nice if expressions could be used inside annotations, i.e. to perform something like the following:

Customer.objects.annotate('total_purchased': Sum(F('customer__order__lineitem__quantity') * F('customer__order__lineitem__price')))

Change History (15)

comment:1 by anonymous, 16 years ago

Version: 1.1-beta-1SVN

comment:2 by Ramiro Morales, 16 years ago

milestone: 1.1

comment:3 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by django@…, 15 years ago

Cc: django@… added

comment:5 by Alexander Koshelev, 15 years ago

Cc: Alexander Koshelev added

comment:6 by miracle2k, 15 years ago

Cc: miracle2k added

comment:7 by miracle2k, 15 years ago

Has patch: set

I've written a basic patch enabling this, against the current trunk:

http://github.com/miracle2k/django/tree/10972-aggrating-expr

There are some things I had trouble with, where I'm definitely not sure what the right thing do to is, for example the way the Where node now needs to handle children who's as_sql() returns a 2-tuple (sql, params):

http://github.com/miracle2k/django/commit/13b0e62772557ad54cd0b51b551ad230784e7eb1#L6L152

Generally though, it seems to work. Maybe this can inspire someone to do a better implementation. A set of simple tests included.

comment:8 by meticulos_slacker, 15 years ago

Cc: meticulos_slacker added

comment:9 by anonymous, 14 years ago

Cc: bgrand added

comment:10 by nwp, 14 years ago

Cc: nwp added
Patch needs improvement: set

comment:11 by Dan Fairs, 14 years ago

Cc: Dan Fairs added

comment:12 by Julien Phalip, 14 years ago

Severity: Normal
Type: New feature

comment:13 by Ramiro Morales, 13 years ago

Description: modified (diff)
Easy pickings: unset
UI/UX: unset

comment:14 by Ramiro Morales, 13 years ago

Resolution: duplicate
Status: newclosed

I'm closing this as a duplicate of #14030 because although this ticker is older, the other one has a wider scope as shown by its Summary and has a comment by a cor developer.

comment:15 by Anssi Kääriäinen, 12 years ago

Component: ORM aggregationDatabase layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top