#21524: Annotation and .extra with group function breaks at database level
-------------------------------------+-------------------------------------
     Reporter:  eu@…                 |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.4
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:  oracle               |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by shai):

 The solution suggested is indeed dirty -- and bogus (I can think up valid
 queries it breaks, the most trivial of which adds constant 'sum()' in the
 extra columns).

 In a similar case, I did something like

 {{{#!python
 def annotate_percents(qset):
     class AnnotatedQuerySet(qset.__class__):
         def iterator(self):
            for obj in super(AnnotatedQuerySet, self).iterator():
                obj.closed_percent = obj.closed/obj.total * 100
                obj.opened_percent = obj.opened/obj.total * 100
                yield obj
    qset.__class__ = AnnotatedQuerySet
    return qset
 }}}

 Now, in the case you have with AVG you may really need to do things in the
 database; you can still do this efficiently by judicious use of
 `prefetch_related`.
 It can allow you to do things that look like you add a new related query
 per object returned, but really only cost you one more database access per
 the whole queryset.

 I hope this helps; note that even if something like your use-case ends up
 supported, it will probably not be in Django 1.4.x.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21524#comment:3>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/074.6baeb9d092fcb2f6418f2a250200f41d%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to