#25028: annotation with Q object duplicates results
-------------------------------+--------------------------------------
     Reporter:  karyon         |                    Owner:  nobody
         Type:  Bug            |                   Status:  closed
    Component:  Uncategorized  |                  Version:  1.8
     Severity:  Normal         |               Resolution:  duplicate
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------

Comment (by akaariai):

 This is a bit different from #10060, as there is no aggregation going on
 here. The problem is that a single user has multiple groups, and you'll
 get a different annotation for each of the groups.

 You'll likely get the correct result if you add
 `filter(Q(groups__name='group1')|Q(groups__isnull=True))` *before* the
 annotate() call. This will work as the user can have at most one group
 named group1.

 I think there would be room for adding a generic query operator for asking
 "annotate True to users which have at least (or at most) N groups matching
 condition X". Your case is "at least 1 group named 'group1'", with the
 extra constraint that group's name is unique. In the more general cases
 answering this question requires aggregation. Notably you could also solve
 your problem by conditional aggregation where you count all groups that
 are named 'group1', and then annotate True if result is greater or equal
 to 1.

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

Reply via email to