mirror of
https://github.com/django/django.git
synced 2025-07-29 14:09:13 +00:00
Fixed #36522 -- Added support for filtering composite pks using a tuple of expressions.
Thanks Jacob Walls for the report, and Sarah Boyce and Mariusz Felisiak for reviews.
This commit is contained in:
parent
fdeca38072
commit
0a4999b422
@ -103,7 +103,11 @@ class TupleLookupMixin:
|
||||
def process_rhs(self, compiler, connection):
|
||||
if self.rhs_is_direct_value():
|
||||
args = [
|
||||
Value(val, output_field=col.output_field)
|
||||
(
|
||||
val
|
||||
if hasattr(val, "as_sql")
|
||||
else Value(val, output_field=col.output_field)
|
||||
)
|
||||
for col, val in zip(self.lhs, self.rhs)
|
||||
]
|
||||
return compiler.compile(Tuple(*args))
|
||||
@ -337,7 +341,11 @@ class TupleIn(TupleLookupMixin, In):
|
||||
result.append(
|
||||
Tuple(
|
||||
*[
|
||||
Value(val, output_field=col.output_field)
|
||||
(
|
||||
val
|
||||
if hasattr(val, "as_sql")
|
||||
else Value(val, output_field=col.output_field)
|
||||
)
|
||||
for col, val in zip(lhs, vals)
|
||||
]
|
||||
)
|
||||
|
@ -12,3 +12,6 @@ Bugfixes
|
||||
* Fixed a regression in Django 5.2.1 that prevented the usage of ``UNNEST``
|
||||
PostgreSQL strategy of ``QuerySet.bulk_create()`` with foreign keys
|
||||
(:ticket:`36502`).
|
||||
|
||||
* Fixed a crash in Django 5.2 when filtering against a composite primary key
|
||||
using a tuple containing expressions (:ticket:`36522`).
|
||||
|
@ -9,6 +9,7 @@ from django.db.models import (
|
||||
Q,
|
||||
Subquery,
|
||||
TextField,
|
||||
Value,
|
||||
When,
|
||||
)
|
||||
from django.db.models.functions import Cast
|
||||
@ -549,6 +550,13 @@ class CompositePKFilterTests(TestCase):
|
||||
[self.tenant_1],
|
||||
)
|
||||
|
||||
def test_filter_by_tuple_containing_expression(self):
|
||||
pk_lookup = (self.comment_1.tenant.id, (Value(self.comment_1.id) + 1) - 1)
|
||||
for lookup in ({"pk": pk_lookup}, {"pk__in": [pk_lookup]}):
|
||||
with self.subTest(lookup=lookup):
|
||||
qs = Comment.objects.filter(**lookup)
|
||||
self.assertEqual(qs.get(), self.comment_1)
|
||||
|
||||
|
||||
@skipUnlessDBFeature("supports_tuple_lookups")
|
||||
class CompositePKFilterTupleLookupFallbackTests(CompositePKFilterTests):
|
||||
|
Loading…
x
Reference in New Issue
Block a user