From 1771998c0978b8fa06f0df3bfce8630b228c4daa Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Mon, 14 Nov 2022 09:01:21 +0100 Subject: [PATCH] Reduced nesting inside Query.get_aggregation(). --- django/db/models/sql/query.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index c858816289d5..fa72c124c723 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -514,14 +514,13 @@ def get_aggregation(self, using, added_aggregate_names): # and move them to the outer AggregateQuery. col_cnt = 0 for alias, expression in list(inner_query.annotation_select.items()): + if not expression.is_summary: + continue annotation_select_mask = inner_query.annotation_select_mask - if expression.is_summary: - expression, col_cnt = inner_query.rewrite_cols(expression, col_cnt) - outer_query.annotations[alias] = expression.relabeled_clone( - relabels - ) - del inner_query.annotations[alias] - annotation_select_mask.remove(alias) + expression, col_cnt = inner_query.rewrite_cols(expression, col_cnt) + outer_query.annotations[alias] = expression.relabeled_clone(relabels) + del inner_query.annotations[alias] + annotation_select_mask.remove(alias) # Make sure the annotation_select wont use cached results. inner_query.set_annotation_mask(inner_query.annotation_select_mask) if (