Skip to content

Commit c1758de

Browse files
committed
Fix isnull issue from Django 4
1 parent b2b2ff3 commit c1758de

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

localized_fields/lookups.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def process_lhs(self, qn, connection):
4343
return super().process_lhs(qn, connection)
4444

4545
def get_prep_lookup(self):
46+
# Django 4.0 removed the ability for isnull fields to be something other than a bool
47+
# We should NOT convert them to strings
48+
if isinstance(self.rhs, bool):
49+
return self.rhs
4650
return str(self.rhs)
4751

4852

tests/test_isnull.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import django
2+
import pytest
3+
4+
from django.test import TestCase
5+
6+
from localized_fields.fields import LocalizedField
7+
from localized_fields.value import LocalizedValue
8+
9+
from .fake_model import get_fake_model
10+
11+
12+
class LocalizedIsNullLookupsTestCase(TestCase):
13+
"""Tests whether ref lookups properly work with."""
14+
15+
TestModel1 = None
16+
17+
@classmethod
18+
def setUpClass(cls):
19+
"""Creates the test model in the database."""
20+
super(LocalizedIsNullLookupsTestCase, cls).setUpClass()
21+
cls.TestModel = get_fake_model(
22+
{"text": LocalizedField(null=True, required=[])}
23+
)
24+
cls.TestModel.objects.create(
25+
text=LocalizedValue(dict(en="text_en", ro="text_ro", nl="text_nl"))
26+
)
27+
cls.TestModel.objects.create(
28+
text=None,
29+
)
30+
31+
def test_isnull_lookup_valid_values(self):
32+
"""Test whether isnull properly works with valid values."""
33+
assert self.TestModel.objects.filter(text__isnull=True).exists()
34+
assert self.TestModel.objects.filter(text__isnull=False).exists()
35+
36+
def test_isnull_lookup_null(self):
37+
"""Test whether isnull crashes with None as value."""
38+
39+
with pytest.raises(ValueError):
40+
assert self.TestModel.objects.filter(text__isnull=None).exists()
41+
42+
def test_isnull_lookup_string(self):
43+
"""Test whether isnull properly works with string values on the
44+
corresponding Django version."""
45+
if django.VERSION < (4, 0):
46+
assert self.TestModel.objects.filter(text__isnull="True").exists()
47+
else:
48+
with pytest.raises(ValueError):
49+
assert self.TestModel.objects.filter(
50+
text__isnull="True"
51+
).exists()

0 commit comments

Comments
 (0)