Skip to content

Commit 0e90fab

Browse files
authored
Merge pull request #6 from cloudblue/fix/LITE-25721
LITE-25721 Fixed null comparison filtering (Django __isnull operator)
2 parents 148669a + 8f43720 commit 0e90fab

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

dj_mongoengine_rql/filter_cls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright © 2022 CloudBlue LLC. All rights reserved.
33
#
44

5+
from dj_rql.constants import DjangoLookups
56
from dj_rql.filter_cls import RQLFilterClass
67
from django_mongoengine.fields.djangoflavor import DjangoField
78
from py_rql.constants import FilterLookups
@@ -39,6 +40,10 @@ def _get_decimal_field_precision(cls, field):
3940
return field.precision
4041

4142
def _build_django_q(self, filter_item, django_lookup, filter_lookup, typed_value):
43+
if django_lookup == DjangoLookups.NULL:
44+
q = self.Q_CLS(**{filter_item['orm_route']: None})
45+
return ~q if filter_lookup == FilterLookups.NE else q
46+
4247
if filter_lookup != FilterLookups.NE:
4348
kwargs = {'{0}__{1}'.format(filter_item['orm_route'], django_lookup): typed_value}
4449
else:

tests/test_filter_cls.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ def test_not():
6161
assert qs.query.q == {'$nor': [{'other_int_f': 120}], 'other_int_f': 1}
6262

6363

64+
def test_null():
65+
_, qs = DocFilterClass(Doc.objects).apply_filters('flt=null()')
66+
67+
assert qs.query.q == {'flt': None}
68+
69+
70+
def test_not_null():
71+
_, qs = DocFilterClass(Doc.objects).apply_filters('flt=ne=null()')
72+
73+
assert qs.query.q == {'$nor': [{'flt': None}]}
74+
75+
6476
def test_db_operation(is_real_mongo):
6577
if is_real_mongo:
6678
doc = Doc.objects.create(str_f='a')

0 commit comments

Comments
 (0)