Skip to content

Commit a947bd2

Browse files
committed
make Value encode Decimal as Decimal128
1 parent 21ed108 commit a947bd2

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

django_mongodb/expressions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from decimal import Decimal
2+
3+
from bson import Decimal128
14
from django.core.exceptions import EmptyResultSet, FullResultSet
25
from django.db.models.expressions import (
36
Case,
@@ -60,7 +63,10 @@ def when(self, compiler, connection):
6063

6164

6265
def value(self, compiler, connection): # noqa: ARG001
63-
return {"$literal": self.value}
66+
value = self.value
67+
if isinstance(value, Decimal):
68+
value = Decimal128(value)
69+
return {"$literal": value}
6470

6571

6672
def register_expressions():

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6161
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_and_alias_filter_in_subquery",
6262
# Length of null considered zero rather than null.
6363
"db_functions.text.test_length.LengthTests.test_basic",
64-
# annotating with Decimal() crashes: bson.errors.InvalidDocument:
65-
# cannot encode object: Decimal('1'), of type: <class 'decimal.Decimal'>
66-
"expressions_case.tests.CaseExpressionTests.test_annotate_filter_decimal",
6764
# Case(..., then=datetime.date()) crashes: bson.errors.InvalidDocument:
6865
# cannot encode object: datetime.date(2024, 5, 14), of type: <class 'datetime.date'>
6966
"expressions_case.tests.CaseDocumentationExamples.test_filter_example",

0 commit comments

Comments
 (0)