diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 75037e4..9a9054e 100644
a
|
b
|
class BaseQuery(object):
|
277 | 277 | """ |
278 | 278 | Performs a COUNT() query using the current filter constraints. |
279 | 279 | """ |
280 | | from subqueries import CountQuery |
| 280 | from django.db.models import Count as AggCount |
281 | 281 | obj = self.clone() |
282 | | obj.clear_ordering(True) |
283 | 282 | obj.clear_limits() |
| 283 | obj.clear_ordering(True) |
284 | 284 | obj.select_related = False |
285 | 285 | obj.related_select_cols = [] |
286 | 286 | obj.related_select_fields = [] |
287 | | if len(obj.select) > 1: |
288 | | obj = self.clone(CountQuery, _query=obj, where=self.where_class(), |
289 | | distinct=False) |
290 | | obj.select = [] |
291 | | obj.aggregate_select = {} |
292 | | obj.extra_select = {} |
293 | | obj.add_count_column() |
294 | | data = obj.execute_sql(SINGLE) |
295 | | if not data: |
296 | | return 0 |
297 | | number = data[0] |
298 | | |
| 287 | obj.add_aggregate(AggCount('pk', distinct=obj.distinct), self.model, 'f', True) |
| 288 | number = obj.get_aggregation()['f'] |
| 289 | |
299 | 290 | # Apply offset and limit constraints manually, since using LIMIT/OFFSET |
300 | 291 | # in SQL (in variants that provide them) doesn't change the COUNT |
301 | 292 | # output. |
diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
index f97608e..76c38ae 100644
a
|
b
|
Traceback (most recent call last):
|
121 | 121 | ... |
122 | 122 | FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, id, isbn, name, pages, price, publisher, store, num_authors |
123 | 123 | |
| 124 | >>> Book.objects.annotate(num_authors=Count('authors')).count() |
| 125 | 6 |
| 126 | |
124 | 127 | """ |
125 | 128 | } |
126 | 129 | |