Django

Code

Changeset 6959

Show
Ignore:
Timestamp:
12/19/07 06:23:59 (7 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Made qs.dates(...).count() work.

This involved a slight change in the SQL for .dates() which appears to be
correct and passes all the tests, but may have some side-effect I don't know
about.

Refs #6203.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/sql/datastructures.py

    r6497 r6959  
    5151        if isinstance(self.col, (list, tuple)): 
    5252            col = ('%s.%s' % tuple([quote_func(c) for c in self.col])) 
     53        elif hasattr(self.col, 'as_sql'): 
     54            col = self.col.as_sql(quote_func) 
    5355        else: 
    5456            col = self.col 
     
    5658            return 'COUNT(DISTINCT %s)' % col 
    5759        else: 
    58             return 'COUNT(%s)' % self.col 
     60            return 'COUNT(%s)' % col 
    5961 
    6062class Date(object): 
  • django/branches/queryset-refactor/django/db/models/sql/query.py

    r6958 r6959  
    194194            obj = self.clone(CountQuery, _query=obj, where=WhereNode(), 
    195195                    distinct=False) 
     196            obj.select = [] 
     197            obj.extra_select = SortedDict() 
    196198        obj.add_count_column() 
    197199        data = obj.execute_sql(SINGLE) 
     
    10091011    def add_count_column(self): 
    10101012        """ 
    1011         Converts the query to do count(*) or count(distinct(pk)) in order to 
     1013        Converts the query to do count(...) or count(distinct(pk)) in order to 
    10121014        get its size. 
    10131015        """ 
     
    10151017        # that it doesn't totally overwrite the select list. 
    10161018        if not self.distinct: 
    1017             select = Count() 
     1019            if not self.select: 
     1020                select = Count() 
     1021            else: 
     1022                assert len(self.select) == 1, \ 
     1023                        "Cannot add count col with multiple cols in 'select': %r" % self.select 
     1024                select = Count(self.select[0]) 
    10181025        else: 
    10191026            opts = self.model._meta 
     
    12301237                self.connection.ops.date_trunc_sql) 
    12311238        self.select = [select] 
     1239        self.distinct = True 
    12321240        self.order_by = order == 'ASC' and [1] or [-1] 
    1233         if self.connection.features.allows_group_by_ordinal: 
    1234             self.group_by = [1] 
    1235         else: 
    1236             self.group_by = [select] 
    12371241 
    12381242class CountQuery(Query): 
  • django/branches/queryset-refactor/tests/regressiontests/queries/models.py

    r6958 r6959  
    22Various complex queries that have been problematic in the past. 
    33""" 
     4 
     5import datetime 
    46 
    57from django.db import models 
     
    4345class Item(models.Model): 
    4446    name = models.CharField(max_length=10) 
     47    created = models.DateTimeField() 
    4548    tags = models.ManyToManyField(Tag, blank=True, null=True) 
    4649    creator = models.ForeignKey(Author) 
     
    130133>>> a4.save() 
    131134 
    132 >>> i1 = Item(name='one', creator=a1, note=n3) 
     135>>> time1 = datetime.datetime(2007, 12, 19, 22, 25, 0) 
     136>>> time2 = datetime.datetime(2007, 12, 19, 21, 0, 0) 
     137>>> time3 = datetime.datetime(2007, 12, 20, 22, 25, 0) 
     138>>> time4 = datetime.datetime(2007, 12, 20, 21, 0, 0) 
     139>>> i1 = Item(name='one', created=time1, creator=a1, note=n3) 
    133140>>> i1.save() 
    134141>>> i1.tags = [t1, t2] 
    135 >>> i2 = Item(name='two', creator=a2, note=n2) 
     142>>> i2 = Item(name='two', created=time2, creator=a2, note=n2) 
    136143>>> i2.save() 
    137144>>> i2.tags = [t1, t3] 
    138 >>> i3 = Item(name='three', creator=a2, note=n3) 
     145>>> i3 = Item(name='three', created=time3, creator=a2, note=n3) 
    139146>>> i3.save() 
    140 >>> i4 = Item(name='four', creator=a4, note=n3) 
     147>>> i4 = Item(name='four', created=time4, creator=a4, note=n3) 
    141148>>> i4.save() 
    142149>>> i4.tags = [t4] 
     
    245252# Create something with a duplicate 'name' so that we can test multi-column 
    246253# cases (which require some tricky SQL transformations under the covers). 
    247 >>> xx = Item(name='four', creator=a2, note=n1) 
     254>>> xx = Item(name='four', created=time1, creator=a2, note=n1) 
    248255>>> xx.save() 
    249256>>> Item.objects.exclude(name='two').values('creator', 'name').distinct().count() 
     
    472479>>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id) 
    473480[<Author: a1>] 
     481 
     482Bug #6203 
     483>>> Item.objects.count() 
     4844 
     485>>> Item.objects.dates('created', 'month').count() 
     4861 
     487>>> Item.objects.dates('created', 'day').count() 
     4882 
     489>>> len(Item.objects.dates('created', 'day')) 
     4902 
    474491"""} 
    475492