Skip to content

Commit f4b6df3

Browse files
committed
fix QuerySet.annotate() crash with DurationField & Decimal computation
1 parent 875c13d commit f4b6df3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7979
# pymongo.errors.OperationFailure: $multiply only supports numeric
8080
# types, not date. (should be wrapped in DatabaseError).
8181
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
82-
# crash in DatabaseOperations.convert_durationfield_value():
83-
# unsupported type for timedelta milliseconds component: Decimal128
84-
"expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide",
8582
}
8683

8784
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.

django_mongodb/operations.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ def convert_decimalfield_value(self, value, expression, connection):
109109

110110
def convert_durationfield_value(self, value, expression, connection):
111111
if value is not None:
112-
value = datetime.timedelta(milliseconds=value)
112+
try:
113+
value = datetime.timedelta(milliseconds=value)
114+
except TypeError:
115+
# `value` could be Decimal128 if doing a computation with
116+
# DurationField and Decimal128.
117+
value = datetime.timedelta(milliseconds=int(str(value)))
113118
return value
114119

115120
def convert_jsonfield_value(self, value, expression, connection):

0 commit comments

Comments
 (0)