Skip to content

Commit 21e11a1

Browse files
committed
fix QuerySet.annotate() crash with DurationField & Decimal computation
1 parent d6cbe81 commit 21e11a1

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
@@ -72,9 +72,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7272
# pymongo.errors.OperationFailure: $multiply only supports numeric
7373
# types, not date. (should be wrapped in DatabaseError).
7474
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
75-
# crash in DatabaseOperations.convert_durationfield_value():
76-
# unsupported type for timedelta milliseconds component: Decimal128
77-
"expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide",
7875
# 'Ref' object has no attribute 'as_mql'.
7976
"expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation",
8077
# AttributeError: pattern_ops

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)