2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
23. 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
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
# An example of a custom manager called "objects".
|
|
|
|
|
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_fun_people(self):
|
|
|
|
return self.filter(fun=True)
|
|
|
|
|
|
|
|
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)
|
2006-05-02 01:31:56 +00:00
|
|
|
fun = models.BooleanField()
|
|
|
|
objects = PersonManager()
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(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
|
|
|
|
2006-05-31 18:45:17 +00:00
|
|
|
# An example of a custom manager that sets get_query_set().
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
class PublishedBookManager(models.Manager):
|
|
|
|
def get_query_set(self):
|
|
|
|
return super(PublishedBookManager, self).get_query_set().filter(is_published=True)
|
|
|
|
|
|
|
|
class Book(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
author = models.CharField(max_length=30)
|
2006-05-02 01:31:56 +00:00
|
|
|
is_published = models.BooleanField()
|
|
|
|
published_objects = PublishedBookManager()
|
|
|
|
authors = models.ManyToManyField(Person, related_name='books')
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
# An example of providing multiple custom managers.
|
|
|
|
|
|
|
|
class FastCarManager(models.Manager):
|
|
|
|
def get_query_set(self):
|
|
|
|
return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|