From a6cf387d6bf286f6cddc1923168f6266e4fabfac Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 31 Jan 2006 00:02:11 +0000 Subject: [PATCH] magic-removal: Implemented Manager.all() and changed Manager so that it doesn't cache. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2194 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/manager.py | 5 +++++ django/db/models/query.py | 21 ++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/django/db/models/manager.py b/django/db/models/manager.py index f0c6a8d0b4..dd7738bddc 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -35,6 +35,7 @@ class Manager(QuerySet): self.creation_counter = Manager.creation_counter Manager.creation_counter += 1 self.model = None + self._use_cache = False def _prepare(self): if self.model._meta.get_latest_by: @@ -53,6 +54,10 @@ class Manager(QuerySet): kwargs['limit'] = 1 return self.get_object(*args, **kwargs) + def all(self): + # Returns a caching QuerySet. + return QuerySet(self.model) + def add(self, **kwargs): kwargs.update(self.core_values) new_obj = self.model(**kwargs) diff --git a/django/db/models/query.py b/django/db/models/query.py index d3525b0be8..be96eecb9f 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -63,11 +63,8 @@ class QuerySet(object): # Dictionary of lookup parameters to apply to every _get_sql_clause(). core_filters = {} - # Subclasses need to provide 'self.model' attribute for this class - # to be able to function. - model = None - - def __init__(self): + def __init__(self, model=None): + self.model = model self._filters = Q(**(self.core_filters)) self._order_by = None # Ordering, e.g. ('date', '-name'). If None, use model's ordering. self._select_related = False # Whether to fill cache for related objects. @@ -79,11 +76,15 @@ class QuerySet(object): self._offset = None # OFFSET clause self._limit = None # LIMIT clause self._result_cache = None + self._use_cache = True ######################## # PYTHON MAGIC METHODS # ######################## + def __repr__(self): + return repr(self._get_data()) + def __len__(self): return len(self._get_data()) @@ -313,10 +314,12 @@ class QuerySet(object): return combined def _get_data(self): - return list(self.iterator()) -# if self._result_cache is None: -# self._result_cache = list(self.iterator()) -# return self._result_cache + if self._use_cache: + if self._result_cache is None: + self._result_cache = list(self.iterator()) + return self._result_cache + else: + return list(self.iterator()) def _get_sql_clause(self, allow_joins): opts = self.model._meta