Skip to content

Commit d0e9cf4

Browse files
committed
fix DatabaseOperations.convert_decimalfield_value() crash on ints
1 parent c008bff commit d0e9cf4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6969
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
7070
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
7171
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
72-
# DatabaseOperations.convert_decimalfield_value() crash:
73-
# 'int' object has no attribute 'to_decimal'.
74-
"expressions.tests.ExpressionsNumericTests.test_filter_decimal_expression",
75-
"expressions.tests.ValueTests.test_output_field_decimalfield",
7672
# pymongo.errors.OperationFailure: $multiply only supports numeric
7773
# types, not date. (should be wrapped in DatabaseError).
7874
"expressions.tests.FTimeDeltaTests.test_invalid_operator",

django_mongodb/operations.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import re
44
import uuid
5+
from decimal import Decimal
56

67
from bson.decimal128 import Decimal128
78
from django.conf import settings
@@ -98,7 +99,12 @@ def convert_datetimefield_value(self, value, expression, connection):
9899
def convert_decimalfield_value(self, value, expression, connection):
99100
if value is not None:
100101
# from Decimal128 to decimal.Decimal()
101-
value = value.to_decimal()
102+
try:
103+
value = value.to_decimal()
104+
except AttributeError:
105+
# `value` could be an integer in the case of an annotation
106+
# like ExpressionWrapper(Value(1), output_field=DecimalField().
107+
return Decimal(value)
102108
return value
103109

104110
def convert_durationfield_value(self, value, expression, connection):

0 commit comments

Comments
 (0)