1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Fixed #35982 -- Made DecimalField.get_db_prep_value() call DatabaseOperations.adapt_decimalfield_value().

Regression in e9814029f5.

Thanks Simon Charette for advice and review.
This commit is contained in:
Tim Graham 2024-12-06 20:44:35 -05:00 committed by Sarah Boyce
parent b0b3024720
commit 1860a1afc9
2 changed files with 18 additions and 4 deletions

View File

@ -1828,9 +1828,8 @@ class DecimalField(Field):
) )
return decimal_value return decimal_value
def get_db_prep_save(self, value, connection): def get_db_prep_value(self, value, connection, prepared=False):
if hasattr(value, "as_sql"): value = super().get_db_prep_value(value, connection, prepared)
return value
return connection.ops.adapt_decimalfield_value( return connection.ops.adapt_decimalfield_value(
self.to_python(value), self.max_digits, self.decimal_places self.to_python(value), self.max_digits, self.decimal_places
) )

View File

@ -1,9 +1,10 @@
import math import math
from decimal import Decimal from decimal import Decimal
from unittest import mock
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import connection, models
from django.test import TestCase from django.test import TestCase
from .models import BigD, Foo from .models import BigD, Foo
@ -48,6 +49,20 @@ class DecimalFieldTests(TestCase):
self.assertIsNone(f.get_prep_value(None)) self.assertIsNone(f.get_prep_value(None))
self.assertEqual(f.get_prep_value("2.4"), Decimal("2.4")) self.assertEqual(f.get_prep_value("2.4"), Decimal("2.4"))
def test_get_db_prep_value(self):
"""
DecimalField.get_db_prep_value() must call
DatabaseOperations.adapt_decimalfield_value().
"""
f = models.DecimalField(max_digits=5, decimal_places=1)
# None of the built-in database backends implement
# adapt_decimalfield_value(), so this must be confirmed with mocking.
with mock.patch.object(
connection.ops.__class__, "adapt_decimalfield_value"
) as adapt_decimalfield_value:
f.get_db_prep_value("2.4", connection)
adapt_decimalfield_value.assert_called_with(Decimal("2.4"), 5, 1)
def test_filter_with_strings(self): def test_filter_with_strings(self):
""" """
Should be able to filter decimal fields using strings (#8023). Should be able to filter decimal fields using strings (#8023).