#29291: Conditional expressions and ~Q queries
-------------------------------------+-------------------------------------
Reporter: Bo | Owner: nobody
Marchman |
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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?
--
Ticket URL: <https://code.djangoproject.com/ticket/29291>
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/047.f1a9fc8c3c1208ac07127ab565a59774%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.