diff --git a/django/db/models/manager.py b/django/db/models/manager.py index b2ed971467..4c6f00232e 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -57,45 +57,51 @@ class Manager(object): # PROXIES TO QUERYSET # ####################### - def all(self): - # Returns a caching QuerySet. + def get_query_set(self): + """Returns a new QuerySet object. Subclasses can override this method + to easily customise the behaviour of the Manager. + """ return QuerySet(self.model) + def all(self): + # Returns a caching QuerySet. + return self.get_query_set() + def count(self): - return QuerySet(self.model).count() + return self.get_query_set().count() 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): - return QuerySet(self.model).delete(*args, **kwargs) + return self.get_query_set().delete(*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): - return QuerySet(self.model).extra(*args, **kwargs) + return self.get_query_set().extra(*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): - return QuerySet(self.model).filter(*args, **kwargs) + return self.get_query_set().filter(*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): - return QuerySet(self.model).iterator(*args, **kwargs) + return self.get_query_set().iterator(*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): - return QuerySet(self.model).select_related(*args, **kwargs) + return self.get_query_set().select_related(*args, **kwargs) def values(self, *args, **kwargs): - return QuerySet(self.model).values(*args, **kwargs) + return self.get_query_set().values(*args, **kwargs) ################# # OTHER METHODS # diff --git a/tests/modeltests/custom_managers/models.py b/tests/modeltests/custom_managers/models.py index 2e3bf4be85..3129e33b89 100644 --- a/tests/modeltests/custom_managers/models.py +++ b/tests/modeltests/custom_managers/models.py @@ -22,7 +22,8 @@ class Person(models.Model): # An example of a custom manager that sets a core_filter on its lookups. 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): title = models.CharField(maxlength=50) @@ -36,7 +37,8 @@ class Book(models.Model): # An example of providing multiple custom managers. 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): name = models.CharField(maxlength=10)