From 0ec134e3404202ab58e24b8049a8b33e40b0104f Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Fri, 11 Aug 2017 19:46:11 +0500 Subject: [PATCH] Simplified calculation of used joins in Query.build_filter(). --- django/db/models/sql/query.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 544e23db0b..5fd1d58831 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -970,25 +970,18 @@ class Query: return self.get_compiler(connection=connection).as_sql() def resolve_lookup_value(self, value, can_reuse, allow_joins): - used_joins = set() if hasattr(value, 'resolve_expression'): - pre_joins = self.alias_refcount.copy() value = value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins) - used_joins = {k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)} elif isinstance(value, (list, tuple)): # The items of the iterable may be expressions and therefore need # to be resolved independently. processed_values = [] for sub_value in value: if hasattr(sub_value, 'resolve_expression'): - pre_joins = self.alias_refcount.copy() processed_values.append( sub_value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins) ) - # The used_joins for a tuple of expressions is the union of - # the used_joins for the individual expressions. - used_joins.update(k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)) - return value, used_joins + return value def solve_lookup_type(self, lookup): """ @@ -1136,7 +1129,9 @@ class Query: if not allow_joins and len(parts) > 1: raise FieldError("Joined field references are not permitted in this query") - value, used_joins = self.resolve_lookup_value(value, can_reuse, allow_joins) + pre_joins = self.alias_refcount.copy() + value = self.resolve_lookup_value(value, can_reuse, allow_joins) + used_joins = {k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)} clause = self.where_class() if reffed_expression: