Skip to content

Commit 27eb10b

Browse files
committed
Add comment to explain logic in cast function.
1 parent 9636af4 commit 27eb10b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

django_mongodb/functions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ def cast(self, compiler, connection):
6262
lhs_mql = process_lhs(self, compiler, connection)[0]
6363
if max_length := self.output_field.max_length:
6464
lhs_mql = {"$substrCP": [lhs_mql, 0, max_length]}
65-
if output_type == "object":
66-
lhs_mql = {"$convert": {"input": lhs_mql, "to": output_type, "onError": lhs_mql}}
67-
else:
65+
66+
# In case of "object", we skip the conversion as it doesn't need to be transformed
67+
# for interpretation by JSONField. JSONField can handle various types,
68+
# including int, object, or array.
69+
if output_type != "object":
6870
lhs_mql = {"$convert": {"input": lhs_mql, "to": output_type}}
71+
6972
if decimal_places := getattr(self.output_field, "decimal_places", None):
7073
lhs_mql = {"$trunc": [lhs_mql, decimal_places]}
7174
return lhs_mql

0 commit comments

Comments
 (0)