Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jacobian jacobian authored
Showing with 11 additions and 1 deletion.
  1. +11 −1 django/db/models/query.py
View
12 django/db/models/query.py
@@ -194,7 +194,17 @@ def iterator(self):
yield obj
def count(self):
- "Performs a SELECT COUNT() and returns the number of records as an integer."
+ """
+ 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 self._results_cache is not None:
+ return len(self._results_cache)
+
counter = self._clone()
counter._order_by = ()
counter._select_related = False
Please sign in to comment.
Something went wrong with that request. Please try again.