magic-removal: extracted usages of QuerySet in Manager to a single method,

which can easily be overridden to create custom managers, and updated 
tests with new method of creating custom managers.


git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2205 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2006-01-31 21:54:28 +00:00
parent 1a53632306
commit 5c3789c908
2 changed files with 24 additions and 16 deletions

View File

@ -57,45 +57,51 @@ class Manager(object):
# PROXIES TO QUERYSET # # PROXIES TO QUERYSET #
####################### #######################
def all(self): def get_query_set(self):
# Returns a caching QuerySet. """Returns a new QuerySet object. Subclasses can override this method
to easily customise the behaviour of the Manager.
"""
return QuerySet(self.model) return QuerySet(self.model)
def all(self):
# Returns a caching QuerySet.
return self.get_query_set()
def count(self): def count(self):
return QuerySet(self.model).count() return self.get_query_set().count()
def dates(self, *args, **kwargs): def dates(self, *args, **kwargs):
return QuerySet(self.model).dates(*args, **kwargs) return self.get_query_set().dates(*args, **kwargs)
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
return QuerySet(self.model).delete(*args, **kwargs) return self.get_query_set().delete(*args, **kwargs)
def distinct(self, *args, **kwargs): def distinct(self, *args, **kwargs):
return QuerySet(self.model).distinct(*args, **kwargs) return self.get_query_set().distinct(*args, **kwargs)
def extra(self, *args, **kwargs): def extra(self, *args, **kwargs):
return QuerySet(self.model).extra(*args, **kwargs) return self.get_query_set().extra(*args, **kwargs)
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
return QuerySet(self.model).get(*args, **kwargs) return self.get_query_set().get(*args, **kwargs)
def filter(self, *args, **kwargs): def filter(self, *args, **kwargs):
return QuerySet(self.model).filter(*args, **kwargs) return self.get_query_set().filter(*args, **kwargs)
def in_bulk(self, *args, **kwargs): def in_bulk(self, *args, **kwargs):
return QuerySet(self.model).in_bulk(*args, **kwargs) return self.get_query_set().in_bulk(*args, **kwargs)
def iterator(self, *args, **kwargs): def iterator(self, *args, **kwargs):
return QuerySet(self.model).iterator(*args, **kwargs) return self.get_query_set().iterator(*args, **kwargs)
def order_by(self, *args, **kwargs): def order_by(self, *args, **kwargs):
return QuerySet(self.model).order_by(*args, **kwargs) return self.get_query_set().order_by(*args, **kwargs)
def select_related(self, *args, **kwargs): def select_related(self, *args, **kwargs):
return QuerySet(self.model).select_related(*args, **kwargs) return self.get_query_set().select_related(*args, **kwargs)
def values(self, *args, **kwargs): def values(self, *args, **kwargs):
return QuerySet(self.model).values(*args, **kwargs) return self.get_query_set().values(*args, **kwargs)
################# #################
# OTHER METHODS # # OTHER METHODS #

View File

@ -22,7 +22,8 @@ class Person(models.Model):
# An example of a custom manager that sets a core_filter on its lookups. # An example of a custom manager that sets a core_filter on its lookups.
class PublishedBookManager(models.Manager): class PublishedBookManager(models.Manager):
core_filters = {'is_published__exact': True} def get_query_set(self):
return super(PublishedBookManager, self).get_query_set().filter(is_published=True)
class Book(models.Model): class Book(models.Model):
title = models.CharField(maxlength=50) title = models.CharField(maxlength=50)
@ -36,7 +37,8 @@ class Book(models.Model):
# An example of providing multiple custom managers. # An example of providing multiple custom managers.
class FastCarManager(models.Manager): class FastCarManager(models.Manager):
core_filters = {'top_speed__gt': 150} def get_query_set(self):
return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150)
class Car(models.Model): class Car(models.Model):
name = models.CharField(maxlength=10) name = models.CharField(maxlength=10)