diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index da34694fed..775dfae772 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -329,7 +329,7 @@ class BaseQuery(object): Performs a COUNT() query using the current filter constraints. """ obj = self.clone() - if len(self.select) > 1: + if len(self.select) > 1 or self.aggregate_select: # If a select clause exists, then the query has already started to # specify the columns that are to be returned. # In this case, we need to use a subquery to evaluate the count. @@ -1950,6 +1950,7 @@ class BaseQuery(object): # Clear out the select cache to reflect the new unmasked aggregates. self.aggregates = {None: count} self.set_aggregate_mask(None) + self.group_by = None def add_select_related(self, fields): """ diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py index f2763d5c04..275ea07390 100644 --- a/tests/regressiontests/aggregation_regress/models.py +++ b/tests/regressiontests/aggregation_regress/models.py @@ -252,6 +252,13 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta >>> [int(x['sheets']) for x in qs] [150, 175, 224, 264, 473, 566] +# Regression for 10425 - annotations don't get in the way of a count() clause +>>> Book.objects.values('publisher').annotate(Count('publisher')).count() +4 + +>>> Book.objects.annotate(Count('publisher')).values('publisher').count() +6 + """ }