[3.1.x] Refs #31829 -- Added DatabaseFeatures.json_key_contains_list_matching_requires_list.

CockroachDB's behavior matches PostgreSQL.
Backport of 184a6eebb0 from master
This commit is contained in:
Tim Graham 2020-07-30 00:38:02 -04:00 committed by Mariusz Felisiak
parent df8696c0b7
commit 32cb1fe1c6
3 changed files with 7 additions and 2 deletions

View File

@ -314,6 +314,9 @@ class BaseDatabaseFeatures:
# Does the backend support __contains and __contained_by lookups for
# a JSONField?
supports_json_field_contains = True
# Does value__d__contains={'f': 'g'} (without a list around the dict) match
# {'d': [{'f': 'g'}]}?
json_key_contains_list_matching_requires_list = False
def __init__(self, connection):
self.connection = connection

View File

@ -59,6 +59,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
validates_explain_options = False # A query will error on invalid options.
supports_deferrable_unique_constraints = True
has_json_operators = True
json_key_contains_list_matching_requires_list = True
@cached_property
def is_postgresql_9_6(self):

View File

@ -699,8 +699,9 @@ class TestQuerying(TestCase):
)),
),
]
# PostgreSQL requires a layer of nesting.
if connection.vendor != 'postgresql':
# For databases where {'f': 'g'} (without surrounding []) matches
# [{'f': 'g'}].
if not connection.features.json_key_contains_list_matching_requires_list:
tests.append(('value__d__contains', {'f': 'g'}))
for lookup, value in tests:
with self.subTest(lookup=lookup, value=value):