#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 Diederik van der Boor):

 > ... either make aggregate return a lazy object that Subquery can deal
 with (right now it returns a dict on call)

 I second that! It would make subqueries much easier to use.
 A lazy dict or `AggregateResult` doesn't have to be too complicated.
 Providing `__getitem__`, `__contains__`, etc.. like `UserDict` would do
 the trick.
 The same could happen with `.count()`. By letting it return a lazy
 `CountResult` object that casts to an int, it doesn't change the existing
 code, but also offers some `.query` attribute that `Subquery()` can use.

 This would allow things like:

 {{{
 Performance.objects.annotate(
     reserved_seats=Subquery(
         SeatReservation.objects.filter(
             performance=OuterRef(name='pk'), status__in=TAKEN_TYPES
         ).aggregate(Count('pk'))
     )
 )
 }}}

 Or even:

 {{{
 Performance.objects.annotate(
     reserved_seats=Subquery(
         SeatReservation.objects.filter(
             performance=OuterRef(name='pk'), status__in=TAKEN_TYPES
         ).count()
     )
 )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28296#comment:7>
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.167f98471ab2158ec9efcbab779383f5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to