2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2014-09-24 05:13:13 +00:00
|
|
|
Giving models a custom manager
|
2006-05-31 18:45:17 +00: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 01:31:56 +00:00
|
|
|
"""
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-09-22 18:23:34 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.db import models
|
2012-08-12 10:32:08 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_fun_people(self):
|
|
|
|
return self.filter(fun=True)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-07-26 08:59:40 +00:00
|
|
|
class PublishedBookManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super(PublishedBookManager, self).get_queryset().filter(is_published=True)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-07-26 08:59:40 +00:00
|
|
|
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
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-07-26 08:59:40 +00:00
|
|
|
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)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-09-18 17:31:07 +00:00
|
|
|
class FunPeopleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super(FunPeopleManager, self).get_queryset().filter(fun=True)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-09-18 17:31:07 +00:00
|
|
|
class BoringPeopleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super(BoringPeopleManager, self).get_queryset().filter(fun=False)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
class Person(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
2013-08-11 20:19:09 +00:00
|
|
|
fun = models.BooleanField(default=False)
|
2013-09-18 17:31:07 +00:00
|
|
|
|
|
|
|
favorite_book = models.ForeignKey('Book', null=True, related_name='favorite_books')
|
|
|
|
favorite_thing_type = models.ForeignKey('contenttypes.ContentType', null=True)
|
|
|
|
favorite_thing_id = models.IntegerField(null=True)
|
2014-01-22 06:43:33 +00:00
|
|
|
favorite_thing = GenericForeignKey('favorite_thing_type', 'favorite_thing_id')
|
2013-09-18 17:31:07 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
objects = PersonManager()
|
2013-09-18 17:31:07 +00:00
|
|
|
fun_people = FunPeopleManager()
|
|
|
|
boring_people = BoringPeopleManager()
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-07-26 08:59:40 +00:00
|
|
|
custom_queryset_default_manager = CustomQuerySet.as_manager()
|
|
|
|
custom_queryset_custom_manager = CustomManager('hello')
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-09-26 23:35:53 +00:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class FunPerson(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
fun = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
favorite_book = models.ForeignKey('Book', null=True, related_name='fun_people_favorite_books')
|
|
|
|
favorite_thing_type = models.ForeignKey('contenttypes.ContentType', null=True)
|
|
|
|
favorite_thing_id = models.IntegerField(null=True)
|
2014-01-22 06:43:33 +00:00
|
|
|
favorite_thing = GenericForeignKey('favorite_thing_type', 'favorite_thing_id')
|
2013-09-26 23:35:53 +00:00
|
|
|
|
|
|
|
objects = FunPeopleManager()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
|
|
|
|
2013-11-27 17:43:37 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
class Book(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
author = models.CharField(max_length=30)
|
2013-08-11 20:19:09 +00:00
|
|
|
is_published = models.BooleanField(default=False)
|
2006-05-02 01:31:56 +00:00
|
|
|
published_objects = PublishedBookManager()
|
|
|
|
authors = models.ManyToManyField(Person, related_name='books')
|
2013-09-26 23:35:53 +00:00
|
|
|
fun_authors = models.ManyToManyField(FunPerson, related_name='books')
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2014-01-22 06:43:33 +00:00
|
|
|
favorite_things = GenericRelation(Person,
|
2013-09-18 17:31:07 +00:00
|
|
|
content_type_field='favorite_thing_type', object_id_field='favorite_thing_id')
|
|
|
|
|
2014-01-22 06:43:33 +00:00
|
|
|
fun_people_favorite_things = GenericRelation(FunPerson,
|
2013-09-26 23:35:53 +00:00
|
|
|
content_type_field='favorite_thing_type', object_id_field='favorite_thing_id')
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.title
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class FastCarManager(models.Manager):
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(FastCarManager, self).get_queryset().filter(top_speed__gt=150)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
class Car(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=10)
|
2006-05-02 01:31:56 +00:00
|
|
|
mileage = models.IntegerField()
|
|
|
|
top_speed = models.IntegerField(help_text="In miles per hour.")
|
|
|
|
cars = models.Manager()
|
|
|
|
fast_cars = FastCarManager()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
2014-09-22 18:23:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RestrictedManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super(RestrictedManager, self).get_queryset().filter(is_public=True)
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RelatedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RestrictedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
is_public = models.BooleanField(default=False)
|
|
|
|
related = models.ForeignKey(RelatedModel)
|
|
|
|
|
|
|
|
objects = RestrictedManager()
|
|
|
|
plain_manager = models.Manager()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class OneToOneRestrictedModel(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
is_public = models.BooleanField(default=False)
|
|
|
|
related = models.OneToOneField(RelatedModel)
|
|
|
|
|
|
|
|
objects = RestrictedManager()
|
|
|
|
plain_manager = models.Manager()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|