summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsage <laymonage@gmail.com>2020-07-26 11:54:40 +0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-07-28 13:10:12 +0200
commit2d8dcba03aae200aaa103ec1e69f0a0038ec2f85 (patch)
tree3b33f725f1dc948029ea2eb51be508eb6477042c
parentba691933cee375195c9c50f333dd4b2a3abbb726 (diff)
Fixed #31829 -- Used JSONField __contains lookup on key transforms.
-rw-r--r--django/db/models/fields/json.py5
-rw-r--r--docs/topics/db/queries.txt14
-rw-r--r--tests/model_fields/test_jsonfield.py28
3 files changed, 33 insertions, 14 deletions
diff --git a/django/db/models/fields/json.py b/django/db/models/fields/json.py
index 7199d86e04..fafc1beee8 100644
--- a/django/db/models/fields/json.py
+++ b/django/db/models/fields/json.py
@@ -431,10 +431,6 @@ class KeyTransformIContains(CaseInsensitiveMixin, KeyTransformTextLookupMixin, l
pass
-class KeyTransformContains(KeyTransformTextLookupMixin, lookups.Contains):
- pass
-
-
class KeyTransformStartsWith(KeyTransformTextLookupMixin, lookups.StartsWith):
pass
@@ -486,7 +482,6 @@ class KeyTransformGte(KeyTransformNumericLookupMixin, lookups.GreaterThanOrEqual
KeyTransform.register_lookup(KeyTransformExact)
KeyTransform.register_lookup(KeyTransformIExact)
KeyTransform.register_lookup(KeyTransformIsNull)
-KeyTransform.register_lookup(KeyTransformContains)
KeyTransform.register_lookup(KeyTransformIContains)
KeyTransform.register_lookup(KeyTransformStartsWith)
KeyTransform.register_lookup(KeyTransformIStartsWith)
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
index a56f9bf078..1b71cd77ff 100644
--- a/docs/topics/db/queries.txt
+++ b/docs/topics/db/queries.txt
@@ -905,10 +905,10 @@ To query for missing keys, use the ``isnull`` lookup::
The lookup examples given above implicitly use the :lookup:`exact` lookup.
Key, index, and path transforms can also be chained with:
- :lookup:`contains`, :lookup:`icontains`, :lookup:`endswith`,
- :lookup:`iendswith`, :lookup:`iexact`, :lookup:`regex`, :lookup:`iregex`,
- :lookup:`startswith`, :lookup:`istartswith`, :lookup:`lt`, :lookup:`lte`,
- :lookup:`gt`, and :lookup:`gte` lookups.
+ :lookup:`icontains`, :lookup:`endswith`, :lookup:`iendswith`,
+ :lookup:`iexact`, :lookup:`regex`, :lookup:`iregex`, :lookup:`startswith`,
+ :lookup:`istartswith`, :lookup:`lt`, :lookup:`lte`, :lookup:`gt`, and
+ :lookup:`gte`, as well as with :ref:`containment-and-key-lookups`.
.. warning::
@@ -937,8 +937,10 @@ To query for missing keys, use the ``isnull`` lookup::
On PostgreSQL, if only one key or index is used, the SQL operator ``->`` is
used. If multiple operators are used then the ``#>`` operator is used.
-Containment and key operations
-------------------------------
+.. _containment-and-key-lookups:
+
+Containment and key lookups
+---------------------------
.. fieldlookup:: jsonfield.contains
diff --git a/tests/model_fields/test_jsonfield.py b/tests/model_fields/test_jsonfield.py
index 4b5ce587f5..11e82c5998 100644
--- a/tests/model_fields/test_jsonfield.py
+++ b/tests/model_fields/test_jsonfield.py
@@ -608,12 +608,29 @@ class TestQuerying(TestCase):
self.objs[3:5],
)
+ @skipUnlessDBFeature('supports_json_field_contains')
+ def test_array_key_contains(self):
+ tests = [
+ ([], [self.objs[7]]),
+ ('bar', [self.objs[7]]),
+ (['bar'], [self.objs[7]]),
+ ('ar', []),
+ ]
+ for value, expected in tests:
+ with self.subTest(value=value):
+ self.assertSequenceEqual(
+ NullableJSONModel.objects.filter(value__bar__contains=value),
+ expected,
+ )
+
def test_key_iexact(self):
self.assertIs(NullableJSONModel.objects.filter(value__foo__iexact='BaR').exists(), True)
self.assertIs(NullableJSONModel.objects.filter(value__foo__iexact='"BaR"').exists(), False)
+ @skipUnlessDBFeature('supports_json_field_contains')
def test_key_contains(self):
- self.assertIs(NullableJSONModel.objects.filter(value__foo__contains='ar').exists(), True)
+ self.assertIs(NullableJSONModel.objects.filter(value__foo__contains='ar').exists(), False)
+ self.assertIs(NullableJSONModel.objects.filter(value__foo__contains='bar').exists(), True)
def test_key_icontains(self):
self.assertIs(NullableJSONModel.objects.filter(value__foo__icontains='Ar').exists(), True)
@@ -670,7 +687,6 @@ class TestQuerying(TestCase):
def test_lookups_with_key_transform(self):
tests = (
- ('value__d__contains', 'e'),
('value__baz__has_key', 'c'),
('value__baz__has_keys', ['a', 'c']),
('value__baz__has_any_keys', ['a', 'x']),
@@ -685,7 +701,10 @@ class TestQuerying(TestCase):
@skipUnlessDBFeature('supports_json_field_contains')
def test_contains_contained_by_with_key_transform(self):
tests = [
+ ('value__d__contains', 'e'),
+ ('value__d__contains', [{'f': 'g'}]),
('value__contains', KeyTransform('bax', 'value')),
+ ('value__baz__contains', {'a': 'b'}),
('value__baz__contained_by', {'a': 'b', 'c': 'd', 'e': 'f'}),
(
'value__contained_by',
@@ -695,8 +714,11 @@ class TestQuerying(TestCase):
)),
),
]
+ # PostgreSQL requires a layer of nesting.
+ if connection.vendor != 'postgresql':
+ tests.append(('value__d__contains', {'f': 'g'}))
for lookup, value in tests:
- with self.subTest(lookup=lookup):
+ with self.subTest(lookup=lookup, value=value):
self.assertIs(NullableJSONModel.objects.filter(
**{lookup: value},
).exists(), True)