From 0c1518ee429b01c145cf5b34eab01b0b92f8c246 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 24 May 2023 18:59:55 +1000 Subject: [PATCH] Fixed #34590 -- Reverted "Refs #33308 -- Improved adapting DecimalField values to decimal." This reverts 7990d254b0af158baf827fafbd90fe8e890f23bd. Thanks Marc Odermatt for the report. --- django/db/models/fields/__init__.py | 6 ++---- docs/releases/4.2.2.txt | 4 ++++ tests/model_fields/test_decimalfield.py | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 18b48c0e72..f958589bea 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1810,13 +1810,11 @@ class DecimalField(Field): ) return decimal_value - def get_db_prep_value(self, value, connection, prepared=False): - if not prepared: - value = self.get_prep_value(value) + def get_db_prep_save(self, value, connection): if hasattr(value, "as_sql"): return value return connection.ops.adapt_decimalfield_value( - value, self.max_digits, self.decimal_places + self.to_python(value), self.max_digits, self.decimal_places ) def get_prep_value(self, value): diff --git a/docs/releases/4.2.2.txt b/docs/releases/4.2.2.txt index df3f2a2710..fef5d8d364 100644 --- a/docs/releases/4.2.2.txt +++ b/docs/releases/4.2.2.txt @@ -36,3 +36,7 @@ Bugfixes * Fixed a regression in Django 4.2 that caused a crash of ``QuerySet.aggregate()`` with aggregates referencing subqueries (:ticket:`34551`). + +* Fixed a regression in Django 4.2 that caused a crash of querysets on SQLite + when filtering on ``DecimalField`` against values outside of the defined + range (:ticket:`34590`). diff --git a/tests/model_fields/test_decimalfield.py b/tests/model_fields/test_decimalfield.py index 88c6878f59..588bb64a3a 100644 --- a/tests/model_fields/test_decimalfield.py +++ b/tests/model_fields/test_decimalfield.py @@ -91,7 +91,10 @@ class DecimalFieldTests(TestCase): Really big values can be used in a filter statement. """ # This should not crash. - Foo.objects.filter(d__gte=100000000000) + self.assertSequenceEqual(Foo.objects.filter(d__gte=100000000000), []) + + def test_lookup_decimal_larger_than_max_digits(self): + self.assertSequenceEqual(Foo.objects.filter(d__lte=Decimal("123456")), []) def test_max_digits_validation(self): field = models.DecimalField(max_digits=2)