Changeset 6959
- Timestamp:
- 12/19/07 06:23:59 (7 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/queryset-refactor/django/db/models/sql/datastructures.py
r6497 r6959 51 51 if isinstance(self.col, (list, tuple)): 52 52 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) 53 55 else: 54 56 col = self.col … … 56 58 return 'COUNT(DISTINCT %s)' % col 57 59 else: 58 return 'COUNT(%s)' % self.col60 return 'COUNT(%s)' % col 59 61 60 62 class Date(object): django/branches/queryset-refactor/django/db/models/sql/query.py
r6958 r6959 194 194 obj = self.clone(CountQuery, _query=obj, where=WhereNode(), 195 195 distinct=False) 196 obj.select = [] 197 obj.extra_select = SortedDict() 196 198 obj.add_count_column() 197 199 data = obj.execute_sql(SINGLE) … … 1009 1011 def add_count_column(self): 1010 1012 """ 1011 Converts the query to do count( *) or count(distinct(pk)) in order to1013 Converts the query to do count(...) or count(distinct(pk)) in order to 1012 1014 get its size. 1013 1015 """ … … 1015 1017 # that it doesn't totally overwrite the select list. 1016 1018 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]) 1018 1025 else: 1019 1026 opts = self.model._meta … … 1230 1237 self.connection.ops.date_trunc_sql) 1231 1238 self.select = [select] 1239 self.distinct = True 1232 1240 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]1237 1241 1238 1242 class CountQuery(Query): django/branches/queryset-refactor/tests/regressiontests/queries/models.py
r6958 r6959 2 2 Various complex queries that have been problematic in the past. 3 3 """ 4 5 import datetime 4 6 5 7 from django.db import models … … 43 45 class Item(models.Model): 44 46 name = models.CharField(max_length=10) 47 created = models.DateTimeField() 45 48 tags = models.ManyToManyField(Tag, blank=True, null=True) 46 49 creator = models.ForeignKey(Author) … … 130 133 >>> a4.save() 131 134 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) 133 140 >>> i1.save() 134 141 >>> i1.tags = [t1, t2] 135 >>> i2 = Item(name='two', creat or=a2, note=n2)142 >>> i2 = Item(name='two', created=time2, creator=a2, note=n2) 136 143 >>> i2.save() 137 144 >>> i2.tags = [t1, t3] 138 >>> i3 = Item(name='three', creat or=a2, note=n3)145 >>> i3 = Item(name='three', created=time3, creator=a2, note=n3) 139 146 >>> i3.save() 140 >>> i4 = Item(name='four', creat or=a4, note=n3)147 >>> i4 = Item(name='four', created=time4, creator=a4, note=n3) 141 148 >>> i4.save() 142 149 >>> i4.tags = [t4] … … 245 252 # Create something with a duplicate 'name' so that we can test multi-column 246 253 # cases (which require some tricky SQL transformations under the covers). 247 >>> xx = Item(name='four', creat or=a2, note=n1)254 >>> xx = Item(name='four', created=time1, creator=a2, note=n1) 248 255 >>> xx.save() 249 256 >>> Item.objects.exclude(name='two').values('creator', 'name').distinct().count() … … 472 479 >>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id) 473 480 [<Author: a1>] 481 482 Bug #6203 483 >>> Item.objects.count() 484 4 485 >>> Item.objects.dates('created', 'month').count() 486 1 487 >>> Item.objects.dates('created', 'day').count() 488 2 489 >>> len(Item.objects.dates('created', 'day')) 490 2 474 491 """} 475 492
