mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #31956 -- Fixed crash of ordering by JSONField with a custom decoder on PostgreSQL.
Thanks Marc Debureaux for the report. Thanks Simon Charette, Nick Pope, and Adam Johnson for reviews.
This commit is contained in:
@@ -153,13 +153,6 @@ class BaseDatabaseOperations:
|
||||
"""
|
||||
return self.date_extract_sql(lookup_type, field_name)
|
||||
|
||||
def json_cast_text_sql(self, field_name):
|
||||
"""Return the SQL to cast a JSON value to text value."""
|
||||
raise NotImplementedError(
|
||||
'subclasses of BaseDatabaseOperations may require a '
|
||||
'json_cast_text_sql() method'
|
||||
)
|
||||
|
||||
def deferrable_sql(self):
|
||||
"""
|
||||
Return the SQL to make a constraint "initially deferred" during a
|
||||
|
||||
@@ -200,7 +200,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||
# Set the isolation level to the value from OPTIONS.
|
||||
if self.isolation_level != connection.isolation_level:
|
||||
connection.set_session(isolation_level=self.isolation_level)
|
||||
|
||||
# Register dummy loads() to avoid a round trip from psycopg2's decode
|
||||
# to json.dumps() to json.loads(), when using a custom decoder in
|
||||
# JSONField.
|
||||
psycopg2.extras.register_default_jsonb(conn_or_curs=connection, loads=lambda x: x)
|
||||
return connection
|
||||
|
||||
def ensure_timezone(self):
|
||||
|
||||
@@ -74,9 +74,6 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||
def time_trunc_sql(self, lookup_type, field_name):
|
||||
return "DATE_TRUNC('%s', %s)::time" % (lookup_type, field_name)
|
||||
|
||||
def json_cast_text_sql(self, field_name):
|
||||
return '(%s)::text' % field_name
|
||||
|
||||
def deferrable_sql(self):
|
||||
return " DEFERRABLE INITIALLY DEFERRED"
|
||||
|
||||
|
||||
@@ -70,8 +70,6 @@ class JSONField(CheckFieldDefaultMixin, Field):
|
||||
def from_db_value(self, value, expression, connection):
|
||||
if value is None:
|
||||
return value
|
||||
if connection.features.has_native_json_field and self.decoder is None:
|
||||
return value
|
||||
try:
|
||||
return json.loads(value, cls=self.decoder)
|
||||
except json.JSONDecodeError:
|
||||
@@ -91,14 +89,6 @@ class JSONField(CheckFieldDefaultMixin, Field):
|
||||
return transform
|
||||
return KeyTransformFactory(name)
|
||||
|
||||
def select_format(self, compiler, sql, params):
|
||||
if (
|
||||
compiler.connection.features.has_native_json_field and
|
||||
self.decoder is not None
|
||||
):
|
||||
return compiler.connection.ops.json_cast_text_sql(sql), params
|
||||
return super().select_format(compiler, sql, params)
|
||||
|
||||
def validate(self, value, model_instance):
|
||||
super().validate(value, model_instance)
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user