#28296: Add support for aggregation through subqueries
-------------------------------------+-------------------------------------
     Reporter:  László Károlyi       |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 I did a bit of investigation to determine the length of changes this would
 require. I started with trying to make `count()` return a lazy object as
 it's a bit easier to implement than `aggregate()`. I didn't implement the
 `resolve_expression` or `as_sql` for now.

 As expected making `count()` lazy breaks a few tests that expect the query
 to be immediately executed but but only a few adjustments to `LazyObject`
 are required to get most of the suite passing.

 Still this breaks backward compatiblity and would certainly break more
 than just test code out there. For example, a view a could be getting a
 count, perfoming some alterations and retrieving a new count and that
 would break if the first query is not immediately executed.

 The work I've done can be found here
 https://github.com/django/django/compare/master...charettes:ticket-28296

 What I suggest we do here instead is to introduce a new
 `AggregateSubQuery(queryset, aggregate)` expression instead. It's
 certainly not as elegant as using `count()` or `aggregate()` but it does
 maintain backward compatiblity.

 e.g.

 {{{#!python
 Performance.objects.annotate(
     reserved_seats=AggregateSubQuery(SeatReservation.objects.filter(
         performance=OuterRef(name='pk'),
         status__in=TAKEN_TYPES,
     ), Count('pk')),
 )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28296#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.103247dd598354dbe184f8333cb1955d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to