2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
23. 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
|
|
|
"""
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# An example of a custom manager called "objects".
|
|
|
|
|
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_fun_people(self):
|
|
|
|
return self.filter(fun=True)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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)
|
2006-05-02 09:31:56 +08:00
|
|
|
fun = models.BooleanField()
|
|
|
|
objects = PersonManager()
|
|
|
|
|
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-03-08 22:15:23 +08:00
|
|
|
# An example of a custom manager that sets get_queryset().
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class PublishedBookManager(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(PublishedBookManager, self).get_queryset().filter(is_published=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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)
|
2006-05-02 09:31:56 +08:00
|
|
|
is_published = models.BooleanField()
|
|
|
|
published_objects = PublishedBookManager()
|
|
|
|
authors = models.ManyToManyField(Person, related_name='books')
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
# An example of providing multiple custom managers.
|
|
|
|
|
|
|
|
class FastCarManager(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(FastCarManager, self).get_queryset().filter(top_speed__gt=150)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|