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)