summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2024-02-15 21:48:42 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-02-16 07:40:33 +0100
commit0d8fbe2ade29f1b7bd9e6ba7a0281f5478603a43 (patch)
tree4e1eb4b91be7c62de00559a8953ea069f816b00d
parentc991602ce5798385261381025c06698d7fd30dc5 (diff)
Refs #34060 -- Fixed crash when filtering against literal JSON with psycopg2.
-rw-r--r--django/db/backends/postgresql/features.py7
-rw-r--r--django/db/backends/postgresql/psycopg_any.py17
-rw-r--r--tests/model_fields/test_jsonfield.py7
3 files changed, 21 insertions, 10 deletions
diff --git a/django/db/backends/postgresql/features.py b/django/db/backends/postgresql/features.py
index 5880390827..7bcc356407 100644
--- a/django/db/backends/postgresql/features.py
+++ b/django/db/backends/postgresql/features.py
@@ -106,13 +106,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"test_group_by_nested_expression_with_params",
}
)
- if not is_psycopg3:
- expected_failures.update(
- {
- "constraints.tests.CheckConstraintTests."
- "test_validate_jsonfield_exact",
- }
- )
return expected_failures
@cached_property
diff --git a/django/db/backends/postgresql/psycopg_any.py b/django/db/backends/postgresql/psycopg_any.py
index 1fe6b15caf..700fc6ec83 100644
--- a/django/db/backends/postgresql/psycopg_any.py
+++ b/django/db/backends/postgresql/psycopg_any.py
@@ -75,9 +75,15 @@ except ImportError:
from enum import IntEnum
from psycopg2 import errors, extensions, sql # NOQA
- from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange, Inet # NOQA
- from psycopg2.extras import Json as Jsonb # NOQA
- from psycopg2.extras import NumericRange, Range # NOQA
+ from psycopg2.extras import ( # NOQA
+ DateRange,
+ DateTimeRange,
+ DateTimeTZRange,
+ Inet,
+ Json,
+ NumericRange,
+ Range,
+ )
RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange)
@@ -101,3 +107,8 @@ except ImportError:
return cursor.mogrify(sql, params).decode()
is_psycopg3 = False
+
+ class Jsonb(Json):
+ def getquoted(self):
+ quoted = super().getquoted()
+ return quoted + b"::jsonb"
diff --git a/tests/model_fields/test_jsonfield.py b/tests/model_fields/test_jsonfield.py
index 1be59e17b3..ff42b1a14c 100644
--- a/tests/model_fields/test_jsonfield.py
+++ b/tests/model_fields/test_jsonfield.py
@@ -1120,3 +1120,10 @@ class TestQuerying(TestCase):
KT("value")
with self.assertRaisesMessage(ValueError, msg):
KT("")
+
+ def test_literal_annotation_filtering(self):
+ all_objects = NullableJSONModel.objects.order_by("id")
+ qs = all_objects.annotate(data=Value({"foo": "bar"}, JSONField())).filter(
+ data__foo="bar"
+ )
+ self.assertQuerySetEqual(qs, all_objects)