0
0
mirror of https://github.com/django/django.git synced 2024-11-21 19:09:18 +01:00

Fixed #34539 -- Restored get_prep_value() call when adapting JSONFields.

Regression in 5c23d9f0c3.
This commit is contained in:
Julie Rymer 2023-05-15 15:19:38 +02:00 committed by Mariusz Felisiak
parent 7414704e88
commit 0ec60661e6
4 changed files with 29 additions and 0 deletions

View File

@ -532,6 +532,7 @@ answer newbie questions, and generally made Django that much better:
Julia Elman
Julia Matsieva <julia.matsieva@gmail.com>
Julian Bez
Julie Rymer <rymerjulie.pro@gmail.com>
Julien Phalip <jphalip@gmail.com>
Junyoung Choi <cupjoo@gmail.com>
junzhang.jn@gmail.com

View File

@ -99,6 +99,8 @@ class JSONField(CheckFieldDefaultMixin, Field):
return "JSONField"
def get_db_prep_value(self, value, connection, prepared=False):
if not prepared:
value = self.get_prep_value(value)
# RemovedInDjango51Warning: When the deprecation ends, replace with:
# if (
# isinstance(value, expressions.Value)

View File

@ -12,3 +12,6 @@ Bugfixes
* Fixed a regression in Django 4.2 that caused an unnecessary
``DBMS_LOB.SUBSTR()`` wrapping in the ``__isnull`` and ``__exact=None``
lookups for ``TextField()``/``BinaryField()`` on Oracle (:ticket:`34544`).
* Restored, following a regression in Django 4.2, ``get_prep_value()`` call in
``JSONField`` subclasses (:ticket:`34539`).

View File

@ -103,6 +103,29 @@ class TestMethods(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg):
KeyTransformTextLookupMixin(transform)
def test_get_prep_value(self):
class JSONFieldGetPrepValue(models.JSONField):
def get_prep_value(self, value):
if value is True:
return {"value": True}
return value
def noop_adapt_json_value(value, encoder):
return value
field = JSONFieldGetPrepValue()
with mock.patch.object(
connection.ops, "adapt_json_value", noop_adapt_json_value
):
self.assertEqual(
field.get_db_prep_value(True, connection, prepared=False),
{"value": True},
)
self.assertIs(
field.get_db_prep_value(True, connection, prepared=True), True
)
self.assertEqual(field.get_db_prep_value(1, connection, prepared=False), 1)
class TestValidation(SimpleTestCase):
def test_invalid_encoder(self):