Fixed #36029 -- Handled implicit exact lookups in condition depth checks for FilteredRelation.

This commit is contained in:
Jacob Walls 2024-12-29 03:14:41 -08:00 committed by Sarah Boyce
parent 45dc2aaa2a
commit c3a681659c
2 changed files with 19 additions and 6 deletions

View File

@ -1704,12 +1704,12 @@ class Query(BaseExpression):
"relations outside the %r (got %r)."
% (filtered_relation.relation_name, lookup)
)
else:
raise ValueError(
"FilteredRelation's condition doesn't support nested "
"relations deeper than the relation_name (got %r for "
"%r)." % (lookup, filtered_relation.relation_name)
)
if len(lookup_field_parts) > len(relation_field_parts) + 1:
raise ValueError(
"FilteredRelation's condition doesn't support nested "
"relations deeper than the relation_name (got %r for "
"%r)." % (lookup, filtered_relation.relation_name)
)
filtered_relation.condition = rename_prefix_from_q(
filtered_relation.relation_name,
alias,

View File

@ -668,6 +668,19 @@ class FilteredRelationTests(TestCase):
),
)
def test_condition_deeper_relation_name_implicit_exact(self):
msg = (
"FilteredRelation's condition doesn't support nested relations "
"deeper than the relation_name (got 'book__editor__name' for 'book')."
)
with self.assertRaisesMessage(ValueError, msg):
Author.objects.annotate(
book_editor=FilteredRelation(
"book",
condition=Q(book__editor__name="b"),
),
)
def test_with_empty_relation_name_error(self):
with self.assertRaisesMessage(ValueError, "relation_name cannot be empty."):
FilteredRelation("", condition=Q(blank=""))