Skip to content

Commit 6279fee

Browse files
committed
fix DatabaseOperations.convert_decimalfield_value() crash on ints
1 parent fe5197c commit 6279fee

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
@@ -99,10 +99,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9999
# 'Col' object has no attribute 'utcoffset'
100100
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
101101
"expressions.tests.IterableLookupInnerExpressionsTests.test_in_lookup_allows_F_expressions_and_expressions_for_datetimes",
102-
# DatabaseOperations.convert_decimalfield_value() crash:
103-
# 'int' object has no attribute 'to_decimal'.
104-
"expressions.tests.ExpressionsNumericTests.test_filter_decimal_expression",
105-
"expressions.tests.ValueTests.test_output_field_decimalfield",
106102
# pymongo.errors.OperationFailure: $multiply only supports numeric
107103
# types, not date. (should be wrapped in DatabaseError).
108104
"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)