Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3541: queryset.count() now respects the queryset cache.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4561 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 16bd0aa99161c4f9646f1546e47b6ac5f9a1e3c2 1 parent ee5415e
Jacob Kaplan-Moss authored February 23, 2007

Showing 1 changed file with 11 additions and 1 deletion. Show diff stats Hide diff stats

  1. 12  django/db/models/query.py
12  django/db/models/query.py
@@ -194,7 +194,17 @@ def iterator(self):
194 194
                 yield obj
195 195
 
196 196
     def count(self):
197  
-        "Performs a SELECT COUNT() and returns the number of records as an integer."
  197
+        """
  198
+        Performs a SELECT COUNT() and returns the number of records as an
  199
+        integer.
  200
+        
  201
+        If the queryset is already cached (i.e. self._result_cache is set) this
  202
+        simply returns the length of the cached results set to avoid multiple
  203
+        SELECT COUNT(*) calls.
  204
+        """
  205
+        if self._results_cache is not None:
  206
+            return len(self._results_cache)
  207
+            
198 208
         counter = self._clone()
199 209
         counter._order_by = ()
200 210
         counter._select_related = False

0 notes on commit 16bd0aa

Please sign in to comment.
Something went wrong with that request. Please try again.