Skip to content

Commit b8c99ca

Browse files
committed
add null existence checks for expr conversions
1 parent 797da9e commit b8c99ca

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

django_mongodb_backend/query_conversion/expression_converters.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ def convert(cls, args):
7676
field_expr, value = args
7777
# Check if first argument is a simple field reference.
7878
if (field_name := cls.convert_path_name(field_expr)) and cls.is_simple_value(value):
79+
query = {"$and": [{"$exists": True}]} if value is None else None
80+
core_check = None
7981
if cls.operator == "$eq":
80-
if value is None:
81-
return {"$and": [{field_name: {"$exists": True}}, {field_name: None}]}
82-
return {field_name: value}
83-
return {field_name: {cls.operator: value}}
82+
core_check = {field_name: value}
83+
if value is None:
84+
core_check = {field_name: {cls.operator: None}}
85+
query = query["$and"].append(core_check) if query else core_check
86+
return query
8487
return None
8588

8689

@@ -133,11 +136,12 @@ def convert(cls, in_args):
133136
field_expr, values = in_args
134137
# Check if first argument is a simple field reference
135138
# Check if second argument is a list of simple values
136-
if (field_name := cls.(field_expr)) and (
139+
if (field_name := cls.convert_path_name(field_expr)) and (
137140
isinstance(values, list | tuple | set)
138141
and all(cls.is_simple_value(v) for v in values)
139142
):
140-
return {field_name: {"$in": values}}
143+
core_check = {field_name: {"$in": values}}
144+
return {"$and": [{"$exists": True}, core_check]} if None in values else core_check
141145
return None
142146

143147

0 commit comments

Comments
 (0)