#30841: Using __isnull lookup to a non-boolean value doesn't promote join.
-------------------------------------+-------------------------------------
Reporter: André Ericson | Owner: André
| Ericson
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by André Ericson):
Replying to [comment:5 felixxm]:
> After the reconsideration I don't think that we should change this
[https://docs.djangoproject.com/en/2.2/ref/models/querysets/#isnull
documented] behavior (that is in Django from the very beginning).
`__isnull` lookup expects boolean values in many places and IMO it would
be confusing if we'll allow for truthy/falsy values, e.g. take a look at
these examples `field__isnull='false'` or `field__isnull='true'` (both
would return the same result). You can always call `bool()` on a right
hand side.
>
> Sorry for my previous acceptation (I shouldn't triage tickets in the
weekend).
I understand your point. But is there anything we can do to avoid people
falling for the same pitfall I did? The problem, in my opinion, is that it
works fine for simple queries but as soon as you add a join that needs
promotion it will break, silently. Maybe we should make it raise an
exception when a non-boolean is passed?
One valid example is to have a class that implements `__bool__`.
You can see here
https://github.com/django/django/blob/d9881a025c15d87b2a7883ee50771117450ea90d/django/db/models/lookups.py#L465-L470
that non-bool value is converted to IS NULL and IS NOT NULL already using
the truthy/falsy values.
> IMO it would be confusing if we'll allow for truthy/falsy values, e.g.
take a look at these examples field__isnull='false' or
field__isnull='true' (both would return the same result).
This is already the case. It just is inconsistent, in `lookups.py`
`field__isnull='false' ` will be a positive condition but on the
`query.py` it will be the negative condition.
--
Ticket URL: <https://code.djangoproject.com/ticket/30841#comment:6>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.f6714f4d7dd6891fb993316cbf75a108%40djangoproject.com.