summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-09-07 13:56:45 -0400
committerGitHub <noreply@github.com>2016-09-07 13:56:45 -0400
commit2eb7d6e6d41480f21305fc6abe2f1a443491ddec (patch)
treefa810985f34787ec5e48090987892044474bd042
parent7b6dccc82fa5b03cf431742c0655e5ac954e228e (diff)
Fixed #26908 -- Fixed crash with jsonfield__key__isnull lookup.
-rw-r--r--django/contrib/postgres/fields/jsonb.py2
-rw-r--r--tests/postgres_tests/test_json.py11
2 files changed, 12 insertions, 1 deletions
diff --git a/django/contrib/postgres/fields/jsonb.py b/django/contrib/postgres/fields/jsonb.py
index e333f30619..1a6ad0831b 100644
--- a/django/contrib/postgres/fields/jsonb.py
+++ b/django/contrib/postgres/fields/jsonb.py
@@ -106,7 +106,7 @@ class KeyTransform(Transform):
lookup = "'%s'" % self.key_name
else:
lookup = "%s" % self.key_name
- return "%s -> %s" % (lhs, lookup), params
+ return "(%s -> %s)" % (lhs, lookup), params
class KeyTransformFactory(object):
diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py
index fd4db7fae4..aeaae0de34 100644
--- a/tests/postgres_tests/test_json.py
+++ b/tests/postgres_tests/test_json.py
@@ -156,6 +156,17 @@ class TestQuerying(TestCase):
[self.objs[0]]
)
+ def test_isnull_key(self):
+ # key__isnull works the same as has_key='key'.
+ self.assertSequenceEqual(
+ JSONModel.objects.filter(field__a__isnull=True),
+ self.objs[:7] + self.objs[9:]
+ )
+ self.assertSequenceEqual(
+ JSONModel.objects.filter(field__a__isnull=False),
+ [self.objs[7], self.objs[8]]
+ )
+
def test_contains(self):
self.assertSequenceEqual(
JSONModel.objects.filter(field__contains={'a': 'b'}),