Refs #30841 -- Made isnull lookup raise ValueError for non-boolean values.
Per deprecation timeline.
This commit is contained in:
parent
12ac4916af
commit
396da8b94c
|
@ -1,6 +1,5 @@
|
||||||
import itertools
|
import itertools
|
||||||
import math
|
import math
|
||||||
import warnings
|
|
||||||
from copy import copy
|
from copy import copy
|
||||||
|
|
||||||
from django.core.exceptions import EmptyResultSet
|
from django.core.exceptions import EmptyResultSet
|
||||||
|
@ -10,7 +9,6 @@ from django.db.models.fields import (
|
||||||
)
|
)
|
||||||
from django.db.models.query_utils import RegisterLookupMixin
|
from django.db.models.query_utils import RegisterLookupMixin
|
||||||
from django.utils.datastructures import OrderedSet
|
from django.utils.datastructures import OrderedSet
|
||||||
from django.utils.deprecation import RemovedInDjango40Warning
|
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.utils.hashable import make_hashable
|
from django.utils.hashable import make_hashable
|
||||||
|
|
||||||
|
@ -508,15 +506,9 @@ class IsNull(BuiltinLookup):
|
||||||
|
|
||||||
def as_sql(self, compiler, connection):
|
def as_sql(self, compiler, connection):
|
||||||
if not isinstance(self.rhs, bool):
|
if not isinstance(self.rhs, bool):
|
||||||
# When the deprecation ends, replace with:
|
raise ValueError(
|
||||||
# raise ValueError(
|
'The QuerySet value for an isnull lookup must be True or '
|
||||||
# 'The QuerySet value for an isnull lookup must be True or '
|
'False.'
|
||||||
# 'False.'
|
|
||||||
# )
|
|
||||||
warnings.warn(
|
|
||||||
'Using a non-boolean value for an isnull lookup is '
|
|
||||||
'deprecated, use True or False instead.',
|
|
||||||
RemovedInDjango40Warning,
|
|
||||||
)
|
)
|
||||||
sql, params = compiler.compile(self.lhs)
|
sql, params = compiler.compile(self.lhs)
|
||||||
if self.rhs:
|
if self.rhs:
|
||||||
|
|
|
@ -3424,11 +3424,6 @@ SQL equivalent:
|
||||||
|
|
||||||
SELECT ... WHERE pub_date IS NULL;
|
SELECT ... WHERE pub_date IS NULL;
|
||||||
|
|
||||||
.. deprecated:: 3.1
|
|
||||||
|
|
||||||
Using non-boolean values as the right-hand side is deprecated, use ``True``
|
|
||||||
or ``False`` instead. In Django 4.0, the exception will be raised.
|
|
||||||
|
|
||||||
.. fieldlookup:: regex
|
.. fieldlookup:: regex
|
||||||
|
|
||||||
``regex``
|
``regex``
|
||||||
|
|
|
@ -268,3 +268,6 @@ See :ref:`deprecated-features-3.1` for details on these changes, including how
|
||||||
to remove usage of these features.
|
to remove usage of these features.
|
||||||
|
|
||||||
* The ``PASSWORD_RESET_TIMEOUT_DAYS`` setting is removed.
|
* The ``PASSWORD_RESET_TIMEOUT_DAYS`` setting is removed.
|
||||||
|
|
||||||
|
* The :lookup:`isnull` lookup no longer allows using non-boolean values as the
|
||||||
|
right-hand side.
|
||||||
|
|
|
@ -9,7 +9,6 @@ from django.db.models import Exists, Max, OuterRef
|
||||||
from django.db.models.functions import Substr
|
from django.db.models.functions import Substr
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
from django.test.utils import isolate_apps
|
from django.test.utils import isolate_apps
|
||||||
from django.utils.deprecation import RemovedInDjango40Warning
|
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
Article, Author, Freebie, Game, IsNullWithNoneAsRHS, Player, Season, Tag,
|
Article, Author, Freebie, Game, IsNullWithNoneAsRHS, Player, Season, Tag,
|
||||||
|
@ -985,15 +984,7 @@ class LookupTests(TestCase):
|
||||||
self.assertEqual(authors.get(), newest_author)
|
self.assertEqual(authors.get(), newest_author)
|
||||||
|
|
||||||
def test_isnull_non_boolean_value(self):
|
def test_isnull_non_boolean_value(self):
|
||||||
# These tests will catch ValueError in Django 4.0 when using
|
msg = 'The QuerySet value for an isnull lookup must be True or False.'
|
||||||
# non-boolean values for an isnull lookup becomes forbidden.
|
|
||||||
# msg = (
|
|
||||||
# 'The QuerySet value for an isnull lookup must be True or False.'
|
|
||||||
# )
|
|
||||||
msg = (
|
|
||||||
'Using a non-boolean value for an isnull lookup is deprecated, '
|
|
||||||
'use True or False instead.'
|
|
||||||
)
|
|
||||||
tests = [
|
tests = [
|
||||||
Author.objects.filter(alias__isnull=1),
|
Author.objects.filter(alias__isnull=1),
|
||||||
Article.objects.filter(author__isnull=1),
|
Article.objects.filter(author__isnull=1),
|
||||||
|
@ -1002,5 +993,5 @@ class LookupTests(TestCase):
|
||||||
]
|
]
|
||||||
for qs in tests:
|
for qs in tests:
|
||||||
with self.subTest(qs=qs):
|
with self.subTest(qs=qs):
|
||||||
with self.assertWarnsMessage(RemovedInDjango40Warning, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
qs.exists()
|
qs.exists()
|
||||||
|
|
Loading…
Reference in New Issue