1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #28453 -- Made __exact=None lookup use transforms.

This commit is contained in:
Sergey Fedoseev 2017-07-31 01:01:45 +05:00 committed by Tim Graham
parent e3bd328b0f
commit 0baea920c8
3 changed files with 20 additions and 1 deletions

View File

@ -984,7 +984,8 @@ class Query:
if value is None:
if lookups[-1] not in ('exact', 'iexact'):
raise ValueError("Cannot use None as a query value")
return True, ['isnull'], used_joins
lookups[-1] = 'isnull'
return True, lookups, used_joins
elif hasattr(value, 'resolve_expression'):
pre_joins = self.alias_refcount.copy()
value = value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins)

View File

@ -43,9 +43,20 @@ class Tag(models.Model):
ordering = ('name', )
class NulledTextField(models.TextField):
pass
@NulledTextField.register_lookup
class NulledTransform(models.Transform):
lookup_name = 'nulled'
template = 'NULL'
class Season(models.Model):
year = models.PositiveSmallIntegerField()
gt = models.IntegerField(null=True, blank=True)
nulled_text_field = NulledTextField(null=True)
def __str__(self):
return str(self.year)

View File

@ -839,3 +839,10 @@ class LookupTests(TestCase):
'<Article: Article 7>'],
ordered=False
)
def test_exact_none_transform(self):
"""Transforms are used for __exact=None."""
Season.objects.create(year=1, nulled_text_field='not null')
self.assertFalse(Season.objects.filter(nulled_text_field__isnull=True))
self.assertTrue(Season.objects.filter(nulled_text_field__nulled__isnull=True))
self.assertTrue(Season.objects.filter(nulled_text_field__nulled__exact=None))