Fixed #28453 -- Made __exact=None lookup use transforms.
This commit is contained in:
parent
e3bd328b0f
commit
0baea920c8
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue