diff --git a/django/db/models/query.py b/django/db/models/query.py index b1921a8e4b..f0a0cf8218 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -280,11 +280,10 @@ class QuerySet(object): Performs a SELECT COUNT() and returns the number of records as an integer. - If the QuerySet is already cached (i.e. self._result_cache is set) this - simply returns the length of the cached results set to avoid multiple - SELECT COUNT(*) calls. + If the QuerySet is already fully cached this simply returns the length + of the cached results set to avoid multiple SELECT COUNT(*) calls. """ - if self._result_cache is not None: + if self._result_cache is not None and not self._iter: return len(self._result_cache) return self.query.get_count() diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index fb66c6214b..84d386dc83 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -830,5 +830,13 @@ another cursor. ... obj.save() ... if i > 10: break +Bug #7759 -- count should work with a partially read result set. +>>> count = Number.objects.count() +>>> qs = Number.objects.all() +>>> for obj in qs: +... qs.count() == count +... break +True + """}