#29291: Conditional expressions and ~Q queries
-------------------------------------+-------------------------------------
     Reporter:  Bo Marchman          |                    Owner:  nobody
         Type:  Bug                  |                   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 rjsparks):

 I think this is just a consequence of the way annotate works on a reverse
 ForeignKey rather than being a bug in the conditional expression.

 {{{
 In [53]: Application.objects.annotate(
     ...:     needs_review=Case(
     ...:         When(~Q(score__reviewed=True), then=V(True)),
     ...:         default=V(False),
     ...:         output_field=BooleanField()
     ...:     )
     ...: )
 Out[53]: <QuerySet [<Application: Application object (1)>, <Application:
 Application object (1)>, <Application: Application object (2)>]>

 In [54]: Application.objects.annotate(
     ...:     needs_review=Case(
     ...:         When(~Q(score__reviewed=True), then=V(True)),
     ...:         default=V(False),
     ...:         output_field=BooleanField()
     ...:     )
     ...: ).values_list('needs_review',flat=True)
 Out[54]: <QuerySet [True, False, True]>
 }}}

 Or more simply:

 {{{
 In [55]:
 Application.objects.annotate(score_has_been_reviewed=F('score__reviewed
     ...: '))
 Out[55]: <QuerySet [<Application: Application object (1)>, <Application:
 Application object (1)>, <Application: Application object (2)>]>

 In [56]:
 Application.objects.annotate(score_has_been_reviewed=F('score__reviewed
     ...: ')).values_list('score_has_been_reviewed',flat=True)
 Out[56]: <QuerySet [False, True, None]>
 }}}

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

Reply via email to