Skip to content

Commit ae6cfd6

Browse files
committed
fix QuerySet.annotate() crash with DurationField & Decimal computation
1 parent df34fcd commit ae6cfd6

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
@@ -102,9 +102,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
102102
# pymongo.errors.OperationFailure: $multiply only supports numeric
103103
# types, not date. (should be wrapped in DatabaseError).
104104
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
105-
# crash in DatabaseOperations.convert_durationfield_value():
106-
# unsupported type for timedelta milliseconds component: Decimal128
107-
"expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide",
108105
# 'Ref' object has no attribute 'as_mql'.
109106
"expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation",
110107
# 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)