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
This commit is contained in:
Adrian Holovaty 2006-01-31 00:02:11 +00:00
parent 836478fa74
commit a6cf387d6b
2 changed files with 17 additions and 9 deletions

View File

@ -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)

View File

@ -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