diff options
| author | Loic Bistuer <loic.bistuer@sixmedia.com> | 2013-09-19 00:31:07 +0700 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-09-25 21:15:59 +0300 |
| commit | 04a2a6b0f9cb6bb98edfe84bf4361216d60a4e38 (patch) | |
| tree | e33452e2b614bbe2f707927ac03f2389f43e512c /tests/custom_managers | |
| parent | 83554b018ef283827c0e7459ab934d447b3419d5 (diff) | |
Fixed #3871 -- Custom managers when traversing reverse relations.
Diffstat (limited to 'tests/custom_managers')
| -rw-r--r-- | tests/custom_managers/models.py | 20 | ||||
| -rw-r--r-- | tests/custom_managers/tests.py | 97 |
2 files changed, 106 insertions, 11 deletions
diff --git a/tests/custom_managers/models.py b/tests/custom_managers/models.py index cba375f4d7..26d848b7c0 100644 --- a/tests/custom_managers/models.py +++ b/tests/custom_managers/models.py @@ -11,6 +11,7 @@ returns. from __future__ import unicode_literals +from django.contrib.contenttypes import generic from django.db import models from django.utils.encoding import python_2_unicode_compatible @@ -63,12 +64,28 @@ class BaseCustomManager(models.Manager): CustomManager = BaseCustomManager.from_queryset(CustomQuerySet) +class FunPeopleManager(models.Manager): + def get_queryset(self): + return super(FunPeopleManager, self).get_queryset().filter(fun=True) + +class BoringPeopleManager(models.Manager): + def get_queryset(self): + return super(BoringPeopleManager, self).get_queryset().filter(fun=False) + @python_2_unicode_compatible class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) fun = models.BooleanField(default=False) + + 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) + favorite_thing = generic.GenericForeignKey('favorite_thing_type', 'favorite_thing_id') + objects = PersonManager() + fun_people = FunPeopleManager() + boring_people = BoringPeopleManager() custom_queryset_default_manager = CustomQuerySet.as_manager() custom_queryset_custom_manager = CustomManager('hello') @@ -84,6 +101,9 @@ class Book(models.Model): published_objects = PublishedBookManager() authors = models.ManyToManyField(Person, related_name='books') + favorite_things = generic.GenericRelation(Person, + content_type_field='favorite_thing_type', object_id_field='favorite_thing_id') + def __str__(self): return self.title diff --git a/tests/custom_managers/tests.py b/tests/custom_managers/tests.py index ff14ad8439..f9a9f33d87 100644 --- a/tests/custom_managers/tests.py +++ b/tests/custom_managers/tests.py @@ -7,10 +7,15 @@ from .models import Person, Book, Car, PersonManager, PublishedBookManager class CustomManagerTests(TestCase): - def test_manager(self): - Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True) - p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False) + def setUp(self): + self.b1 = Book.published_objects.create( + title="How to program", author="Rodney Dangerfield", is_published=True) + self.b2 = Book.published_objects.create( + title="How to be smart", author="Albert Einstein", is_published=False) + self.p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True) + self.p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False) + def test_manager(self): # Test a custom `Manager` method. self.assertQuerysetEqual( Person.objects.get_fun_people(), [ @@ -61,14 +66,8 @@ class CustomManagerTests(TestCase): # The RelatedManager used on the 'books' descriptor extends the default # manager - self.assertIsInstance(p2.books, PublishedBookManager) + self.assertIsInstance(self.p2.books, PublishedBookManager) - 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. @@ -76,7 +75,7 @@ class CustomManagerTests(TestCase): # The RelatedManager used on the 'authors' descriptor extends the # default manager - self.assertIsInstance(b2.authors, PersonManager) + self.assertIsInstance(self.b2.authors, PersonManager) self.assertQuerysetEqual( Book.published_objects.all(), [ @@ -114,3 +113,79 @@ class CustomManagerTests(TestCase): ], lambda c: c.name ) + + def test_related_manager_fk(self): + self.p1.favorite_book = self.b1 + self.p1.save() + self.p2.favorite_book = self.b1 + self.p2.save() + + self.assertQuerysetEqual( + self.b1.favorite_books.order_by('first_name').all(), [ + "Bugs", + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.favorite_books(manager='boring_people').all(), [ + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.favorite_books(manager='fun_people').all(), [ + "Bugs", + ], + lambda c: c.first_name + ) + + def test_related_manager_gfk(self): + self.p1.favorite_thing = self.b1 + self.p1.save() + self.p2.favorite_thing = self.b1 + self.p2.save() + + self.assertQuerysetEqual( + self.b1.favorite_things.order_by('first_name').all(), [ + "Bugs", + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.favorite_things(manager='boring_people').all(), [ + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.favorite_things(manager='fun_people').all(), [ + "Bugs", + ], + lambda c: c.first_name + ) + + def test_related_manager_m2m(self): + self.b1.authors.add(self.p1) + self.b1.authors.add(self.p2) + + self.assertQuerysetEqual( + self.b1.authors.order_by('first_name').all(), [ + "Bugs", + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.authors(manager='boring_people').all(), [ + "Droopy", + ], + lambda c: c.first_name + ) + self.assertQuerysetEqual( + self.b1.authors(manager='fun_people').all(), [ + "Bugs", + ], + lambda c: c.first_name + ) |
