From 43179e41ae21e0e314f541c460e10831a870fab3 Mon Sep 17 00:00:00 2001
From: Adrian Holovaty <adrian@holovaty.com>
Date: Mon, 30 Jan 2006 02:57:04 +0000
Subject: [PATCH] magic-removal: Restored caching on QuerySet

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2165 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/db/models/query.py | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/django/db/models/query.py b/django/db/models/query.py
index e4857329cc..068a7c1214 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -72,27 +72,29 @@ class QuerySet(object):
         self._order_by = ()          # Ordering, e.g. ('date', '-name')
         self._select_related = False # Whether to fill cache for related objects.
         self._distinct = False       # Whether the query should use SELECT DISTINCT.
-#         self._result_cache = None
         self._select = None          # Dictionary of attname -> SQL.
         self._where = None           # List of extra WHERE clauses to use.
         self._params = None          # List of params to use for extra WHERE clauses.
         self._tables = None          # List of extra tables to use.
         self._offset = None          # OFFSET clause
         self._limit = None           # LIMIT clause
-#         self._use_cache = False
+        self._result_cache = None
 
     ########################
     # PYTHON MAGIC METHODS #
     ########################
 
-#     def __len__(self):
-#         return len(list(self))
-
-    ###########################################
-    # PUBLIC METHODS THAT DO DATABASE QUERIES #
-    ###########################################
+    def __len__(self):
+        return len(self._get_data())
 
     def __iter__(self):
+        return iter(self._get_data())
+
+    ####################################
+    # METHODS THAT DO DATABASE QUERIES #
+    ####################################
+
+    def iterator(self):
         "Performs the SELECT database lookup of this QuerySet."
         # self._select is a dictionary, and dictionaries' key order is
         # undefined, so we convert it to a list of tuples.
@@ -177,6 +179,11 @@ class QuerySet(object):
         c._limit = self._limit
         return c
 
+    def _get_data(self):
+        if self._result_cache is None:
+            self._result_cache = list(self.iterator())
+        return self._result_cache
+
     def _get_sql_clause(self, allow_joins):
         opts = self.klass._meta