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): diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py index 35677d1524..4422cb1a6a 100644 --- a/tests/modeltests/many_to_many/models.py +++ b/tests/modeltests/many_to_many/models.py @@ -82,6 +82,13 @@ API_TESTS = """ >>> Article.objects.filter(publications__title__startswith="Science").distinct() [NASA uses Python] +# The count() function respects distinct() as well. +>>> Article.objects.filter(publications__title__startswith="Science").count() +2 + +>>> Article.objects.filter(publications__title__startswith="Science").distinct().count() +1 + # Reverse m2m queries are supported (i.e., starting at the table that doesn't # have a ManyToManyField). >>> Publication.objects.filter(id__exact=1) diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py index 165a36c91c..3a0972b793 100644 --- a/tests/modeltests/many_to_one/models.py +++ b/tests/modeltests/many_to_one/models.py @@ -205,6 +205,12 @@ John Smith >>> Reporter.objects.filter(article__headline__startswith='This').distinct() [John Smith] +# Counting in the opposite direction works in conjunction with distinct() +>>> Reporter.objects.filter(article__headline__startswith='This').count() +3 +>>> Reporter.objects.filter(article__headline__startswith='This').distinct().count() +1 + # Queries can go round in circles. >>> Reporter.objects.filter(article__reporter__first_name__startswith='John') [John Smith, John Smith, John Smith, John Smith]