1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #34060 -- Fixed crash when filtering against literal JSON with psycopg2.

This commit is contained in:
Simon Charette 2024-02-15 21:48:42 -05:00 committed by Mariusz Felisiak
parent c991602ce5
commit 0d8fbe2ade
3 changed files with 21 additions and 10 deletions

View File

@ -106,13 +106,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"test_group_by_nested_expression_with_params", "test_group_by_nested_expression_with_params",
} }
) )
if not is_psycopg3:
expected_failures.update(
{
"constraints.tests.CheckConstraintTests."
"test_validate_jsonfield_exact",
}
)
return expected_failures return expected_failures
@cached_property @cached_property

View File

@ -75,9 +75,15 @@ except ImportError:
from enum import IntEnum from enum import IntEnum
from psycopg2 import errors, extensions, sql # NOQA from psycopg2 import errors, extensions, sql # NOQA
from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange, Inet # NOQA from psycopg2.extras import ( # NOQA
from psycopg2.extras import Json as Jsonb # NOQA DateRange,
from psycopg2.extras import NumericRange, Range # NOQA DateTimeRange,
DateTimeTZRange,
Inet,
Json,
NumericRange,
Range,
)
RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange) RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange)
@ -101,3 +107,8 @@ except ImportError:
return cursor.mogrify(sql, params).decode() return cursor.mogrify(sql, params).decode()
is_psycopg3 = False is_psycopg3 = False
class Jsonb(Json):
def getquoted(self):
quoted = super().getquoted()
return quoted + b"::jsonb"

View File

@ -1120,3 +1120,10 @@ class TestQuerying(TestCase):
KT("value") KT("value")
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
KT("") 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)