From 6a970a8b4600eb91be25f38caed0a52269d6303d Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Tue, 20 Jul 2021 10:59:21 +0100 Subject: [PATCH] Fixed #32944 -- Avoided unnecessary WhereNode.add() calls. Co-authored-by: Mariusz Felisiak --- django/contrib/contenttypes/fields.py | 5 ++--- django/db/models/sql/query.py | 10 +++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 4aa3ddaf98..35df584dbc 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -14,6 +14,7 @@ from django.db.models.fields.related import ( ReverseManyToOneDescriptor, lazy_related_operation, ) from django.db.models.query_utils import PathInfo +from django.db.models.sql import AND from django.utils.functional import cached_property @@ -468,10 +469,8 @@ class GenericRelation(ForeignObject): def get_extra_restriction(self, where_class, alias, remote_alias): field = self.remote_field.model._meta.get_field(self.content_type_field_name) contenttype_pk = self.get_content_type().pk - cond = where_class() lookup = field.get_lookup('exact')(field.get_col(remote_alias), contenttype_pk) - cond.add(lookup, 'AND') - return cond + return where_class([lookup], connector=AND) def bulk_related_objects(self, objs, using=DEFAULT_DB_ALIAS): """ diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index b3d92d786c..8837881579 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1265,9 +1265,7 @@ class Query(BaseExpression): condition = filter_expr.resolve_expression(self, allow_joins=allow_joins) if not isinstance(condition, Lookup): condition = self.build_lookup(['exact'], condition, True) - clause = self.where_class() - clause.add(condition, AND) - return clause, [] + return self.where_class([condition], connector=AND), [] arg, value = filter_expr if not arg: raise FieldError("Cannot parse keyword query %r" % arg) @@ -1286,11 +1284,9 @@ class Query(BaseExpression): if check_filterable: self.check_filterable(value) - clause = self.where_class() if reffed_expression: condition = self.build_lookup(lookups, reffed_expression, value) - clause.add(condition, AND) - return clause, [] + return self.where_class([condition], connector=AND), [] opts = self.get_meta() alias = self.get_initial_alias() @@ -1333,7 +1329,7 @@ class Query(BaseExpression): condition = self.build_lookup(lookups, col, value) lookup_type = condition.lookup_name - clause.add(condition, AND) + clause = self.where_class([condition], connector=AND) require_outer = lookup_type == 'isnull' and condition.rhs is True and not current_negated if current_negated and (lookup_type != 'isnull' or condition.rhs is False) and condition.rhs is not None: