diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-01-14 00:18:30 -0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-02-06 14:27:51 +0100 |
| commit | 2598b371a93e21d84b7a2a99b2329535c8c0c138 (patch) | |
| tree | abaf32fc5bb448677232f2dbb6b46ddc42a07ab2 /django/db/models/sql | |
| parent | db7b1ae9f6d9e26facbb3da4cf5f5a513704bfe5 (diff) | |
Fixed #35677 -- Avoided non-sticky filtering of prefetched many-to-many.
The original queryset._next_is_sticky() call never had the intended effect as
no further filtering was applied internally after the pk__in lookup making it
a noop.
In order to be coherent with how related filters are applied when retrieving
objects from a related manager the effects of what calling _next_is_sticky()
prior to applying annotations and filters to the queryset provided for
prefetching are emulated by allowing the reuse of all pre-existing JOINs.
Thanks David Glenck and Thiago Bellini Ribeiro for the detailed reports and
tests.
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/query.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ddf6c891fa..ec47d9aa24 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1616,7 +1616,7 @@ class Query(BaseExpression): def add_filter(self, filter_lhs, filter_rhs): self.add_q(Q((filter_lhs, filter_rhs))) - def add_q(self, q_object): + def add_q(self, q_object, reuse_all=False): """ A preprocessor for the internal _add_q(). Responsible for doing final join promotion. @@ -1630,7 +1630,11 @@ class Query(BaseExpression): existing_inner = { a for a in self.alias_map if self.alias_map[a].join_type == INNER } - clause, _ = self._add_q(q_object, self.used_aliases) + if reuse_all: + can_reuse = set(self.alias_map) + else: + can_reuse = self.used_aliases + clause, _ = self._add_q(q_object, can_reuse) if clause: self.where.add(clause, AND) self.demote_joins(existing_inner) |
