Skip to content

Commit 346549e

Browse files
committed
fix QuerySet.annotate() crash with DurationField & Decimal computation
1 parent cd7ecc2 commit 346549e

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
@@ -67,9 +67,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6767
# pymongo.errors.OperationFailure: $multiply only supports numeric
6868
# types, not date. (should be wrapped in DatabaseError).
6969
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
70-
# crash in DatabaseOperations.convert_durationfield_value():
71-
# unsupported type for timedelta milliseconds component: Decimal128
72-
"expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide",
7370
# 'Ref' object has no attribute 'as_mql'.
7471
"expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation",
7572
# 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)