From 16bd0aa99161c4f9646f1546e47b6ac5f9a1e3c2 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Fri, 23 Feb 2007 20:58:28 +0000 Subject: [PATCH] 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 --- django/db/models/query.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 2209521d93..e16a35cce9 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -194,7 +194,17 @@ class QuerySet(object): 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