mirror of
https://github.com/django/django.git
synced 2025-10-29 00:26:07 +00:00
Fixed #1530 -- make count() respect distinct() on QuerySets. Create some
tests for this as well. Thanks to Adam Endicott for the original patch on which this is based. git-svn-id: http://code.djangoproject.com/svn/django/trunk@2902 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -182,7 +182,12 @@ class QuerySet(object):
|
||||
counter._select_related = False
|
||||
select, sql, params = counter._get_sql_clause()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT COUNT(*)" + sql, params)
|
||||
if self._distinct:
|
||||
id_col = "%s.%s" % (backend.quote_name(self.model._meta.db_table),
|
||||
backend.quote_name(self.model._meta.pk.column))
|
||||
cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
|
||||
else:
|
||||
cursor.execute("SELECT COUNT(*)" + sql, params)
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user