diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2006-05-14 23:49:29 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2006-05-14 23:49:29 +0000 |
| commit | 1006d6eb06e202a5060d7346973a3dbc3bd0ec53 (patch) | |
| tree | 0d7ed66a3dd335ce04c7102413441801816f0381 /django | |
| parent | aa11b3ea503ba1e6247e1629aa13f2752f32e3a9 (diff) | |
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
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index ea7533a67d..b8fc0c695f 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -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): |
