Opened 7 years ago

Closed 5 years ago

Last modified 3 years ago

#10972 closed New feature (duplicate)

Use Expressions with Annotations

Reported by: me@… Owned by:
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: expression aggregation aggregates annotation
Cc: samt@…, django@…, alexkoshelev, miracle2k, meticulos_slacker, bgrand, nwp, danfairs 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)

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 Changed 7 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from 1.1-beta-1 to SVN

comment:2 Changed 7 years ago by ramiro

  • milestone 1.1 deleted

comment:3 Changed 7 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 7 years ago by django@…

  • Cc django@… added

comment:5 Changed 7 years ago by alexkoshelev

  • Cc alexkoshelev added

comment:6 Changed 7 years ago by miracle2k

  • Cc miracle2k added

comment:7 Changed 7 years ago by miracle2k

  • Has patch set

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

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

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

comment:8 Changed 7 years ago by meticulos_slacker

  • Cc meticulos_slacker added

comment:9 Changed 6 years ago by anonymous

  • Cc bgrand added

comment:10 Changed 6 years ago by nwp

  • Cc nwp added
  • Patch needs improvement set

comment:11 Changed 6 years ago by danfairs

  • Cc danfairs added

comment:12 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:13 Changed 5 years ago by ramiro

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

comment:14 Changed 5 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from new to closed

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 Changed 3 years ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top