summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2024-12-29 03:14:41 -0800
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-01-03 08:40:13 +0100
commitc3a681659c4a982101ffee0d3ef205ac5b310e17 (patch)
tree4cb75cc47d44b77b44baf405bc2b4d9b8b38504e
parent45dc2aaa2a9dfe6d7d706f5e9f6757318423602e (diff)
Fixed #36029 -- Handled implicit exact lookups in condition depth checks for FilteredRelation.
-rw-r--r--django/db/models/sql/query.py12
-rw-r--r--tests/filtered_relation/tests.py13
2 files changed, 19 insertions, 6 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index cca11bfcc2..6fbf854e67 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -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,
diff --git a/tests/filtered_relation/tests.py b/tests/filtered_relation/tests.py
index 82caba8662..cbf77752df 100644
--- a/tests/filtered_relation/tests.py
+++ b/tests/filtered_relation/tests.py
@@ -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=""))