Skip to content

store DecimalField as Decimal128 #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"lookup.tests.LookupTests.test_exact_none_transform",
# "Save with update_fields did not affect any rows."
"basic.tests.SelectOnSaveTests.test_select_on_save_lying_update",
# filtering on large decimalfield, see https://code.djangoproject.com/ticket/34590
# for some background.
"model_fields.test_decimalfield.DecimalFieldTests.test_lookup_decimal_larger_than_max_digits",
"model_fields.test_decimalfield.DecimalFieldTests.test_lookup_really_big_value",
# 'TruncDate' object has no attribute 'as_mql'.
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
Expand Down
9 changes: 7 additions & 2 deletions django_mongodb/operations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import decimal
import uuid

from bson.decimal128 import Decimal128
from django.conf import settings
from django.db.backends.base.operations import BaseDatabaseOperations
from django.utils import timezone
Expand All @@ -21,6 +21,10 @@ def adapt_datetimefield_value(self, value):
value = timezone.make_aware(value)
return value

def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
"""Store DecimalField as Decimal128."""
return Decimal128(value)

def adapt_timefield_value(self, value):
"""Store TimeField as datetime."""
if value is None:
Expand Down Expand Up @@ -56,7 +60,8 @@ def convert_datetimefield_value(self, value, expression, connection):

def convert_decimalfield_value(self, value, expression, connection):
if value is not None:
value = decimal.Decimal(value)
# from Decimal128 to decimal.Decimal()
value = value.to_decimal()
return value

def convert_timefield_value(self, value, expression, connection):
Expand Down