mirror of
https://github.com/django/django.git
synced 2025-10-29 00:26:07 +00:00
[1.8.x] Fixed #24705 -- Fixed negated Q objects in expressions.
Avoided split_exclude() for Q when used as an expression.
Backport of bc87061a3c from master
This commit is contained in:
committed by
Tim Graham
parent
63d60dfe29
commit
db65660928
@@ -88,7 +88,7 @@ class Q(tree.Node):
|
||||
# We must promote any new joins to left outer joins so that when Q is
|
||||
# used as an expression, rows aren't filtered due to joins.
|
||||
joins_before = query.tables[:]
|
||||
clause, joins = query._add_q(self, reuse, allow_joins=allow_joins)
|
||||
clause, joins = query._add_q(self, reuse, allow_joins=allow_joins, split_subq=False)
|
||||
joins_to_promote = [j for j in joins if j not in joins_before]
|
||||
query.promote_joins(joins_to_promote)
|
||||
return clause
|
||||
|
||||
@@ -1112,7 +1112,7 @@ class Query(object):
|
||||
(name, lhs.output_field.__class__.__name__))
|
||||
|
||||
def build_filter(self, filter_expr, branch_negated=False, current_negated=False,
|
||||
can_reuse=None, connector=AND, allow_joins=True):
|
||||
can_reuse=None, connector=AND, allow_joins=True, split_subq=True):
|
||||
"""
|
||||
Builds a WhereNode for a single filter clause, but doesn't add it
|
||||
to this Query. Query.add_q() will then add this filter to the where
|
||||
@@ -1161,7 +1161,7 @@ class Query(object):
|
||||
|
||||
opts = self.get_meta()
|
||||
alias = self.get_initial_alias()
|
||||
allow_many = not branch_negated
|
||||
allow_many = not branch_negated or not split_subq
|
||||
|
||||
try:
|
||||
field, sources, opts, join_list, path = self.setup_joins(
|
||||
@@ -1309,7 +1309,7 @@ class Query(object):
|
||||
self.demote_joins(existing_inner)
|
||||
|
||||
def _add_q(self, q_object, used_aliases, branch_negated=False,
|
||||
current_negated=False, allow_joins=True):
|
||||
current_negated=False, allow_joins=True, split_subq=True):
|
||||
"""
|
||||
Adds a Q-object to the current filter.
|
||||
"""
|
||||
@@ -1323,12 +1323,14 @@ class Query(object):
|
||||
if isinstance(child, Node):
|
||||
child_clause, needed_inner = self._add_q(
|
||||
child, used_aliases, branch_negated,
|
||||
current_negated, allow_joins)
|
||||
current_negated, allow_joins, split_subq)
|
||||
joinpromoter.add_votes(needed_inner)
|
||||
else:
|
||||
child_clause, needed_inner = self.build_filter(
|
||||
child, can_reuse=used_aliases, branch_negated=branch_negated,
|
||||
current_negated=current_negated, connector=connector, allow_joins=allow_joins)
|
||||
current_negated=current_negated, connector=connector,
|
||||
allow_joins=allow_joins, split_subq=split_subq,
|
||||
)
|
||||
joinpromoter.add_votes(needed_inner)
|
||||
target_clause.add(child_clause, connector)
|
||||
needed_inner = joinpromoter.update_join_types(self)
|
||||
|
||||
Reference in New Issue
Block a user