Author: mtredinnick
Date: 2008-07-16 18:17:29 -0500 (Wed, 16 Jul 2008)
New Revision: 7938

Modified:
   django/trunk/django/db/models/query.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Fixed #7759 -- Fixed QuerySet.count() when the results cache was only partially
populated.


Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2008-07-16 21:12:43 UTC (rev 
7937)
+++ django/trunk/django/db/models/query.py      2008-07-16 23:17:29 UTC (rev 
7938)
@@ -280,11 +280,10 @@
         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()

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2008-07-16 
21:12:43 UTC (rev 7937)
+++ django/trunk/tests/regressiontests/queries/models.py        2008-07-16 
23:17:29 UTC (rev 7938)
@@ -830,5 +830,13 @@
 ...     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
+
 """}
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to