mirror of
https://github.com/django/django.git
synced 2024-12-22 09:05:43 +00:00
Fixed #35135 -- Made FilteredRelation raise ValueError on querysets as rhs.
Regression in 59f4754704
.
This commit is contained in:
parent
a5365339ea
commit
820c5f1bac
@ -91,6 +91,8 @@ def get_children_from_q(q):
|
|||||||
|
|
||||||
|
|
||||||
def get_child_with_renamed_prefix(prefix, replacement, child):
|
def get_child_with_renamed_prefix(prefix, replacement, child):
|
||||||
|
from django.db.models.query import QuerySet
|
||||||
|
|
||||||
if isinstance(child, Node):
|
if isinstance(child, Node):
|
||||||
return rename_prefix_from_q(prefix, replacement, child)
|
return rename_prefix_from_q(prefix, replacement, child)
|
||||||
if isinstance(child, tuple):
|
if isinstance(child, tuple):
|
||||||
@ -105,6 +107,14 @@ def get_child_with_renamed_prefix(prefix, replacement, child):
|
|||||||
child = child.copy()
|
child = child.copy()
|
||||||
if child.name.startswith(prefix + LOOKUP_SEP):
|
if child.name.startswith(prefix + LOOKUP_SEP):
|
||||||
child.name = child.name.replace(prefix, replacement, 1)
|
child.name = child.name.replace(prefix, replacement, 1)
|
||||||
|
elif isinstance(child, QuerySet):
|
||||||
|
# QuerySet may contain OuterRef() references which cannot work properly
|
||||||
|
# without repointing to the filtered annotation and will spawn a
|
||||||
|
# different JOIN. Always raise ValueError instead of providing partial
|
||||||
|
# support in other cases.
|
||||||
|
raise ValueError(
|
||||||
|
"Passing a QuerySet within a FilteredRelation is not supported."
|
||||||
|
)
|
||||||
elif hasattr(child, "resolve_expression"):
|
elif hasattr(child, "resolve_expression"):
|
||||||
child = child.copy()
|
child = child.copy()
|
||||||
child.set_source_expressions(
|
child.set_source_expressions(
|
||||||
|
@ -19,3 +19,8 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed a bug in Django 5.0 that caused a crash of ``Model.full_clean()`` on
|
* Fixed a bug in Django 5.0 that caused a crash of ``Model.full_clean()`` on
|
||||||
models with a ``GeneratedField`` (:ticket:`35127`).
|
models with a ``GeneratedField`` (:ticket:`35127`).
|
||||||
|
|
||||||
|
* Fixed a regression in Django 5.0 that caused a crash of
|
||||||
|
``FilteredRelation()`` with querysets as right-hand sides (:ticket:`35135`).
|
||||||
|
``FilteredRelation()`` now raises a ``ValueError`` on querysets as right-hand
|
||||||
|
sides.
|
||||||
|
@ -828,6 +828,16 @@ class FilteredRelationTests(TestCase):
|
|||||||
).filter(rel__isnull=True)
|
).filter(rel__isnull=True)
|
||||||
self.assertSequenceEqual(qs, [])
|
self.assertSequenceEqual(qs, [])
|
||||||
|
|
||||||
|
def test_conditional_expression_does_not_support_queryset(self):
|
||||||
|
msg = "Passing a QuerySet within a FilteredRelation is not supported."
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
Author.objects.annotate(
|
||||||
|
poem_book=FilteredRelation(
|
||||||
|
"book",
|
||||||
|
condition=Q(book__in=Book.objects.filter(title__istartswith="a")),
|
||||||
|
),
|
||||||
|
).filter(poem_book__isnull=False)
|
||||||
|
|
||||||
|
|
||||||
class FilteredRelationAggregationTests(TestCase):
|
class FilteredRelationAggregationTests(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user