#29291: Conditional expressions and ~Q queries
-------------------------------------+-------------------------------------
     Reporter:  Bo Marchman          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  2.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Bo Marchman:

Old description:

> I've run into an issue with conditional expressions using negated Q
> objects.
>
> I would expect `qs1` and `qs2` to be equivalent in the following code:
>
> {{{#!python
>
> class Application(models.Model):
>     pass
>
> class Score(models.Model):
>     application = models.ForeignKey(Application,
> on_delete=models.PROTECT)
>     reviewed = models.BooleanField()
>
> a1 = Application.objects.create()
> Score.objects.create(reviewed=False, application=a1)
> Score.objects.create(reviewed=True, application=a1)
>
> a2 = Application.objects.create()
>
> qs1 = Application.objects.annotate(
>     needs_review=Case(
>         When(~Q(score__reviewed=True), then=V(True)),
>         default=V(False),
>         output_field=BooleanField()
>     )
> ).filter(needs_review=True)
>
> qs2 = Application.objects.filter(
>     ~Q(score__reviewed=True)
> )
>
> print(qs1) # <QuerySet [<Application: Application object (1)>,
> <Application: Application object (2)>]>
> print(qs2) # <QuerySet [<Application: Application object (2)>]>
>
> assert set(qs1) == set(qs2)
> }}}
>
> The identical `~Q` expression is behaving differently in the context of
> `Case/When` and `filter`. Am I completely missing something and this is
> expected behavior?

New description:

 I've run into an issue with conditional expressions using negated Q
 objects.

 I would expect `qs1` and `qs2` to be equivalent in the following code:

 {{{#!python

 class Application(models.Model):
     pass

 class Score(models.Model):
     application = models.ForeignKey(Application, on_delete=models.PROTECT)
     reviewed = models.BooleanField()

 a1 = Application.objects.create()
 Score.objects.create(reviewed=False, application=a1)
 Score.objects.create(reviewed=True, application=a1)

 a2 = Application.objects.create()

 qs1 = Application.objects.annotate(
     needs_review=Case(
         When(~Q(score__reviewed=True), then=V(True)),
         default=V(False),
         output_field=BooleanField()
     )
 ).filter(needs_review=True)

 qs2 = Application.objects.filter(
     ~Q(score__reviewed=True)
 )

 print(qs1) # <QuerySet [<Application: Application object (1)>,
 <Application: Application object (2)>]>
 print(qs2) # <QuerySet [<Application: Application object (2)>]>

 assert set(qs1) == set(qs2)
 }}}

 The identical `~Q` expression is behaving differently in the context of
 `Case/When` and `filter`. Am I completely missing something and this is
 expected behavior?

 This is using Django 2.0.4.

--

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

Reply via email to