Ticket #9797: annotate-count.diff

File annotate-count.diff, 1.9 KB (added by Alex Gaynor, 16 years ago)

this fails with both dates and values

  • django/db/models/sql/query.py

    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):  
    277277        """
    278278        Performs a COUNT() query using the current filter constraints.
    279279        """
    280         from subqueries import CountQuery
     280        from django.db.models import Count as AggCount
    281281        obj = self.clone()
    282         obj.clear_ordering(True)
    283282        obj.clear_limits()
     283        obj.clear_ordering(True)
    284284        obj.select_related = False
    285285        obj.related_select_cols = []
    286286        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       
    299290        # Apply offset and limit constraints manually, since using LIMIT/OFFSET
    300291        # in SQL (in variants that provide them) doesn't change the COUNT
    301292        # output.
  • tests/regressiontests/aggregation_regress/models.py

    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):  
    121121...
    122122FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, id, isbn, name, pages, price, publisher, store, num_authors
    123123
     124>>> Book.objects.annotate(num_authors=Count('authors')).count()
     1256
     126
    124127"""
    125128}
    126129
Back to Top