Code

Opened 5 years ago

Closed 3 years ago

Last modified 17 months 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')))

Attachments (0)

Change History (15)

comment:1 Changed 5 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 5 years ago by ramiro

  • milestone 1.1 deleted

comment:3 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 years ago by django@…

  • Cc django@… added

comment:5 Changed 5 years ago by alexkoshelev

  • Cc alexkoshelev added

comment:6 Changed 5 years ago by miracle2k

  • Cc miracle2k added

comment:7 Changed 5 years ago by miracle2k

  • 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 Changed 5 years ago by meticulos_slacker

  • Cc meticulos_slacker added

comment:9 Changed 4 years ago by anonymous

  • Cc bgrand added

comment:10 Changed 4 years ago by nwp

  • Cc nwp added
  • Patch needs improvement set

comment:11 Changed 3 years ago by danfairs

  • Cc danfairs added

comment:12 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:13 Changed 3 years ago by ramiro

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

comment:14 Changed 3 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 17 months ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.