2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Giving models a custom manager
|
2006-06-01 02:45:17 +08:00
|
|
|
|
|
|
|
You can use a custom ``Manager`` in a particular model by extending the base
|
|
|
|
``Manager`` class and instantiating your custom ``Manager`` in your model.
|
|
|
|
|
|
|
|
There are two reasons you might want to customize a ``Manager``: to add extra
|
|
|
|
``Manager`` methods, and/or to modify the initial ``QuerySet`` the ``Manager``
|
|
|
|
returns.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.contenttypes.fields import (
|
|
|
|
GenericForeignKey, GenericRelation,
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_fun_people(self):
|
|
|
|
return self.filter(fun=True)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-07-26 16:59:40 +08:00
|
|
|
class PublishedBookManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(is_published=True)
|
2013-07-26 16:59:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2018-01-14 10:58:21 +08:00
|
|
|
class AnnotatedBookManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().annotate(
|
|
|
|
favorite_avg=models.Avg('favorite_books__favorite_thing_id')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-07-26 16:59:40 +08:00
|
|
|
class CustomQuerySet(models.QuerySet):
|
|
|
|
def filter(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
queryset = super().filter(fun=True)
|
2013-07-26 16:59:40 +08:00
|
|
|
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
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-07-26 16:59:40 +08:00
|
|
|
class BaseCustomManager(models.Manager):
|
|
|
|
def __init__(self, arg):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2013-07-26 16:59:40 +08:00
|
|
|
self.init_arg = arg
|
|
|
|
|
|
|
|
def filter(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
queryset = super().filter(fun=True)
|
2013-07-26 16:59:40 +08:00
|
|
|
queryset._filter_CustomManager = True
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def manager_only(self):
|
|
|
|
return self.all()
|
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
|
2013-07-26 16:59:40 +08:00
|
|
|
CustomManager = BaseCustomManager.from_queryset(CustomQuerySet)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-06-04 07:13:39 +08:00
|
|
|
class CustomInitQuerySet(models.QuerySet):
|
|
|
|
# QuerySet with an __init__() method that takes an additional argument.
|
|
|
|
def __init__(self, custom_optional_arg=None, model=None, query=None, using=None, hints=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(model=model, query=query, using=using, hints=hints)
|
2015-06-04 07:13:39 +08:00
|
|
|
|
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
class DeconstructibleCustomManager(BaseCustomManager.from_queryset(CustomQuerySet)):
|
|
|
|
|
|
|
|
def __init__(self, a, b, c=1, d=2):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(a)
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
class FunPeopleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(fun=True)
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
class BoringPeopleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(fun=False)
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Person(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
2013-08-12 04:19:09 +08:00
|
|
|
fun = models.BooleanField(default=False)
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
favorite_book = models.ForeignKey('Book', models.SET_NULL, null=True, related_name='favorite_books')
|
|
|
|
favorite_thing_type = models.ForeignKey('contenttypes.ContentType', models.SET_NULL, null=True)
|
2013-09-19 01:31:07 +08:00
|
|
|
favorite_thing_id = models.IntegerField(null=True)
|
2014-01-22 14:43:33 +08:00
|
|
|
favorite_thing = GenericForeignKey('favorite_thing_type', 'favorite_thing_id')
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
objects = PersonManager()
|
2013-09-19 01:31:07 +08:00
|
|
|
fun_people = FunPeopleManager()
|
|
|
|
boring_people = BoringPeopleManager()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-26 16:59:40 +08:00
|
|
|
custom_queryset_default_manager = CustomQuerySet.as_manager()
|
|
|
|
custom_queryset_custom_manager = CustomManager('hello')
|
2015-06-04 07:13:39 +08:00
|
|
|
custom_init_queryset_manager = CustomInitQuerySet.as_manager()
|
2013-07-26 16:59:40 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-09-27 07:35:53 +08:00
|
|
|
class FunPerson(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
fun = models.BooleanField(default=True)
|
2015-07-22 22:43:21 +08:00
|
|
|
favorite_book = models.ForeignKey(
|
|
|
|
'Book',
|
|
|
|
models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name='fun_people_favorite_books',
|
|
|
|
)
|
|
|
|
favorite_thing_type = models.ForeignKey('contenttypes.ContentType', models.SET_NULL, null=True)
|
2013-09-27 07:35:53 +08:00
|
|
|
favorite_thing_id = models.IntegerField(null=True)
|
2014-01-22 14:43:33 +08:00
|
|
|
favorite_thing = GenericForeignKey('favorite_thing_type', 'favorite_thing_id')
|
2013-09-27 07:35:53 +08:00
|
|
|
|
|
|
|
objects = FunPeopleManager()
|
|
|
|
|
2013-11-28 01:43:37 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Book(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
author = models.CharField(max_length=30)
|
2013-08-12 04:19:09 +08:00
|
|
|
is_published = models.BooleanField(default=False)
|
2006-05-02 09:31:56 +08:00
|
|
|
authors = models.ManyToManyField(Person, related_name='books')
|
2013-09-27 07:35:53 +08:00
|
|
|
fun_authors = models.ManyToManyField(FunPerson, related_name='books')
|
2016-04-08 10:04:45 +08:00
|
|
|
favorite_things = GenericRelation(
|
|
|
|
Person,
|
|
|
|
content_type_field='favorite_thing_type',
|
|
|
|
object_id_field='favorite_thing_id',
|
|
|
|
)
|
|
|
|
fun_people_favorite_things = GenericRelation(
|
|
|
|
FunPerson,
|
|
|
|
content_type_field='favorite_thing_type',
|
|
|
|
object_id_field='favorite_thing_id',
|
|
|
|
)
|
2013-09-27 07:35:53 +08:00
|
|
|
|
2018-01-14 10:58:21 +08:00
|
|
|
published_objects = PublishedBookManager()
|
|
|
|
annotated_objects = AnnotatedBookManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
base_manager_name = 'annotated_objects'
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2021-08-01 19:13:35 +08:00
|
|
|
class ConfusedBook(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
author = models.CharField(max_length=30)
|
|
|
|
favorite_things = GenericRelation(
|
|
|
|
Person,
|
|
|
|
content_type_field='favorite_thing_type',
|
|
|
|
object_id_field='favorite_thing_id',
|
|
|
|
)
|
|
|
|
less_favorite_things = GenericRelation(
|
|
|
|
FunPerson,
|
|
|
|
content_type_field='favorite_thing_type',
|
|
|
|
object_id_field='favorite_thing_id',
|
|
|
|
related_query_name='favorite_things',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class FastCarManager(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(top_speed__gt=150)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Car(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=10)
|
2006-05-02 09:31:56 +08:00
|
|
|
mileage = models.IntegerField()
|
|
|
|
top_speed = models.IntegerField(help_text="In miles per hour.")
|
|
|
|
cars = models.Manager()
|
|
|
|
fast_cars = FastCarManager()
|
|
|
|
|
2014-09-23 02:23:34 +08:00
|
|
|
|
2016-04-17 19:55:55 +08:00
|
|
|
class FastCarAsBase(Car):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
base_manager_name = 'fast_cars'
|
|
|
|
|
|
|
|
|
|
|
|
class FastCarAsDefault(Car):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
default_manager_name = 'fast_cars'
|
|
|
|
|
|
|
|
|
2014-09-23 02:23:34 +08:00
|
|
|
class RestrictedManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(is_public=True)
|
2014-09-23 02:23:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RelatedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
|
|
|
|
class RestrictedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
is_public = models.BooleanField(default=False)
|
2015-07-22 22:43:21 +08:00
|
|
|
related = models.ForeignKey(RelatedModel, models.CASCADE)
|
2014-09-23 02:23:34 +08:00
|
|
|
|
|
|
|
objects = RestrictedManager()
|
|
|
|
plain_manager = models.Manager()
|
|
|
|
|
|
|
|
|
|
|
|
class OneToOneRestrictedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
is_public = models.BooleanField(default=False)
|
2015-07-22 22:43:21 +08:00
|
|
|
related = models.OneToOneField(RelatedModel, models.CASCADE)
|
2014-09-23 02:23:34 +08:00
|
|
|
|
|
|
|
objects = RestrictedManager()
|
|
|
|
plain_manager = models.Manager()
|
|
|
|
|
2015-06-18 22:13:04 +08:00
|
|
|
|
|
|
|
class AbstractPerson(models.Model):
|
|
|
|
abstract_persons = models.Manager()
|
|
|
|
objects = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class PersonFromAbstract(AbstractPerson):
|
|
|
|
pass
|