mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #20625 -- Chainable Manager/QuerySet methods.
Additionally this patch solves the orthogonal problem that specialized `QuerySet` like `ValuesQuerySet` didn't inherit from the current `QuerySet` type. This wasn't an issue until now because we didn't officially support custom `QuerySet` but it became necessary with the introduction of this new feature. Thanks aaugustin, akaariai, carljm, charettes, mjtamlyn, shaib and timgraham for the reviews.
This commit is contained in:
parent
8f3aefdec3
commit
31fadc1202
10 changed files with 390 additions and 127 deletions
|
@ -20,6 +20,49 @@ class PersonManager(models.Manager):
|
|||
def get_fun_people(self):
|
||||
return self.filter(fun=True)
|
||||
|
||||
# An example of a custom manager that sets get_queryset().
|
||||
|
||||
class PublishedBookManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(PublishedBookManager, self).get_queryset().filter(is_published=True)
|
||||
|
||||
# An example of a custom queryset that copies its methods onto the manager.
|
||||
|
||||
class CustomQuerySet(models.QuerySet):
|
||||
def filter(self, *args, **kwargs):
|
||||
queryset = super(CustomQuerySet, self).filter(fun=True)
|
||||
queryset._filter_CustomQuerySet = True
|
||||
return queryset
|
||||
|
||||
def public_method(self, *args, **kwargs):
|
||||
return self.all()
|
||||
|
||||
def _private_method(self, *args, **kwargs):
|
||||
return self.all()
|
||||
|
||||
def optout_public_method(self, *args, **kwargs):
|
||||
return self.all()
|
||||
optout_public_method.queryset_only = True
|
||||
|
||||
def _optin_private_method(self, *args, **kwargs):
|
||||
return self.all()
|
||||
_optin_private_method.queryset_only = False
|
||||
|
||||
class BaseCustomManager(models.Manager):
|
||||
def __init__(self, arg):
|
||||
super(BaseCustomManager, self).__init__()
|
||||
self.init_arg = arg
|
||||
|
||||
def filter(self, *args, **kwargs):
|
||||
queryset = super(BaseCustomManager, self).filter(fun=True)
|
||||
queryset._filter_CustomManager = True
|
||||
return queryset
|
||||
|
||||
def manager_only(self):
|
||||
return self.all()
|
||||
|
||||
CustomManager = BaseCustomManager.from_queryset(CustomQuerySet)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
|
@ -27,15 +70,12 @@ class Person(models.Model):
|
|||
fun = models.BooleanField()
|
||||
objects = PersonManager()
|
||||
|
||||
custom_queryset_default_manager = CustomQuerySet.as_manager()
|
||||
custom_queryset_custom_manager = CustomManager('hello')
|
||||
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
# An example of a custom manager that sets get_queryset().
|
||||
|
||||
class PublishedBookManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(PublishedBookManager, self).get_queryset().filter(is_published=True)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=50)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue