diff options
Diffstat (limited to 'parts/django/tests/modeltests/custom_managers')
3 files changed, 130 insertions, 0 deletions
diff --git a/parts/django/tests/modeltests/custom_managers/__init__.py b/parts/django/tests/modeltests/custom_managers/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/parts/django/tests/modeltests/custom_managers/__init__.py diff --git a/parts/django/tests/modeltests/custom_managers/models.py b/parts/django/tests/modeltests/custom_managers/models.py new file mode 100644 index 0000000..1052552 --- /dev/null +++ b/parts/django/tests/modeltests/custom_managers/models.py @@ -0,0 +1,59 @@ +""" +23. Giving models a custom manager + +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. +""" + +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): + first_name = models.CharField(max_length=30) + last_name = models.CharField(max_length=30) + fun = models.BooleanField() + objects = PersonManager() + + def __unicode__(self): + return u"%s %s" % (self.first_name, self.last_name) + +# An example of a custom manager that sets get_query_set(). + +class PublishedBookManager(models.Manager): + def get_query_set(self): + return super(PublishedBookManager, self).get_query_set().filter(is_published=True) + +class Book(models.Model): + title = models.CharField(max_length=50) + author = models.CharField(max_length=30) + is_published = models.BooleanField() + published_objects = PublishedBookManager() + authors = models.ManyToManyField(Person, related_name='books') + + def __unicode__(self): + 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): + name = models.CharField(max_length=10) + mileage = models.IntegerField() + top_speed = models.IntegerField(help_text="In miles per hour.") + cars = models.Manager() + fast_cars = FastCarManager() + + def __unicode__(self): + return self.name diff --git a/parts/django/tests/modeltests/custom_managers/tests.py b/parts/django/tests/modeltests/custom_managers/tests.py new file mode 100644 index 0000000..8721e9a --- /dev/null +++ b/parts/django/tests/modeltests/custom_managers/tests.py @@ -0,0 +1,71 @@ +from django.test import TestCase + +from models import Person, Book, Car, PersonManager, PublishedBookManager + + +class CustomManagerTests(TestCase): + def test_manager(self): + p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True) + p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False) + + self.assertQuerysetEqual( + Person.objects.get_fun_people(), [ + "Bugs Bunny" + ], + unicode + ) + # The RelatedManager used on the 'books' descriptor extends the default + # manager + self.assertTrue(isinstance(p2.books, PublishedBookManager)) + + b1 = Book.published_objects.create( + title="How to program", author="Rodney Dangerfield", is_published=True + ) + b2 = Book.published_objects.create( + title="How to be smart", author="Albert Einstein", is_published=False + ) + + # The default manager, "objects", doesn't exist, because a custom one + # was provided. + self.assertRaises(AttributeError, lambda: Book.objects) + + # The RelatedManager used on the 'authors' descriptor extends the + # default manager + self.assertTrue(isinstance(b2.authors, PersonManager)) + + self.assertQuerysetEqual( + Book.published_objects.all(), [ + "How to program", + ], + lambda b: b.title + ) + + c1 = Car.cars.create(name="Corvette", mileage=21, top_speed=180) + c2 = Car.cars.create(name="Neon", mileage=31, top_speed=100) + + self.assertQuerysetEqual( + Car.cars.order_by("name"), [ + "Corvette", + "Neon", + ], + lambda c: c.name + ) + + self.assertQuerysetEqual( + Car.fast_cars.all(), [ + "Corvette", + ], + lambda c: c.name + ) + + # Each model class gets a "_default_manager" attribute, which is a + # reference to the first manager defined in the class. In this case, + # it's "cars". + + self.assertQuerysetEqual( + Car._default_manager.order_by("name"), [ + "Corvette", + "Neon", + ], + lambda c: c.name + ) |