Skip to content

Commit e172f77

Browse files
committed
fix DatabaseOperations.convert_decimalfield_value() crash on ints
1 parent b407269 commit e172f77

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
@@ -76,10 +76,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7676
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
7777
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
7878
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
79-
# DatabaseOperations.convert_decimalfield_value() crash:
80-
# 'int' object has no attribute 'to_decimal'.
81-
"expressions.tests.ExpressionsNumericTests.test_filter_decimal_expression",
82-
"expressions.tests.ValueTests.test_output_field_decimalfield",
8379
# pymongo.errors.OperationFailure: $multiply only supports numeric
8480
# types, not date. (should be wrapped in DatabaseError).
8581
"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)