From a2c67934edb0fb07fbf530bbdb8890646d1bec7b Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 5 Oct 2010 08:59:25 -0400 Subject: [PATCH] additional test, docs cleanup --- CHANGES.rst | 3 +++ README.rst | 20 +++++++------------- model_utils/tests/models.py | 3 +++ model_utils/tests/tests.py | 19 ++++++++++++------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index cbd103c..dcb91bb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ CHANGES tip (unreleased) ---------------- +- added InheritanceCastManager and InheritanceCastQuerySet, to allow bulk + casting of a queryset to child types. Thanks Gregor Müllegger. + 0.5.0 (2010.09.24) ------------------ diff --git a/README.rst b/README.rst index b2605a9..9a4f9cb 100644 --- a/README.rst +++ b/README.rst @@ -251,23 +251,17 @@ return an instance of the proper subtype, ``Restaurant`` or ``Bar``:: restaurant_or_bar = place.cast() # ... -.. note:: - This is inefficient for large querysets, as it results in n - queries to the subtype tables. You can use the ``cast()`` method - on a queryset to reduce this to as many queries as subtypes are - involved. - -:: +This is inefficient for large querysets, as it results in a new query for every +individual returned object. You can use the ``cast()`` method on a queryset to +reduce this to as many queries as subtypes are involved:: nearby_places = Place.objects.filter(location='here') - for pace in nearby_places.cast(): + for place in nearby_places.cast(): # ... -.. note:: - The ``cast()`` method on a queryset does *not* return another - queryset but an already evaluated result of the database query. - This means that you cannot chain any athor queryset methods after - you have called ``cast()``. +.. note:: The ``cast()`` queryset method does *not* return another + queryset but an already evaluated result of the database query. This means + that you cannot chain additional queryset methods after ``cast()``. TimeStampedModel ================ diff --git a/model_utils/tests/models.py b/model_utils/tests/models.py index fa7ff9d..aea8244 100644 --- a/model_utils/tests/models.py +++ b/model_utils/tests/models.py @@ -12,6 +12,9 @@ class InheritParent(InheritanceCastModel): class InheritChild(InheritParent): pass +class InheritChild2(InheritParent): + pass + class TimeStamp(TimeStampedModel): pass diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 4d6a436..455d1aa 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -12,9 +12,11 @@ from model_utils import ChoiceEnum, Choices from model_utils.fields import get_excerpt, MonitorField from model_utils.managers import QueryManager, manager_from from model_utils.models import StatusModel, TimeFramedModel -from model_utils.tests.models import (InheritParent, InheritChild, TimeStamp, - Post, Article, Status, StatusPlainTuple, TimeFrame, Monitored, - StatusManagerAdded, TimeFrameManagerAdded, Entry) +from model_utils.tests.models import (InheritParent, InheritChild, InheritChild2, + TimeStamp, Post, Article, Status, + StatusPlainTuple, TimeFrame, Monitored, + StatusManagerAdded, TimeFrameManagerAdded, + Entry) class GetExcerptTests(TestCase): @@ -228,17 +230,20 @@ class InheritanceCastModelTests(TestCase): class InheritanceCastQuerysetTests(TestCase): def setUp(self): self.child = InheritChild.objects.create() + self.child2 = InheritChild2.objects.create() def test_cast_manager(self): - obj = InheritParent.objects.cast()[0] - self.assertEquals(obj.__class__, InheritChild) + self.assertEquals(set(InheritParent.objects.cast()), + set([self.child, self.child2])) def test_cast(self): parent = InheritParent.objects.create() obj = InheritParent.objects.filter(pk=self.child.pk).cast()[0] self.assertEquals(obj.__class__, InheritChild) - obj = InheritChild.objects.all().cast()[0] - self.assertEquals(obj.__class__, InheritChild) + self.assertEquals(set(InheritChild2.objects.all().cast()), + set([self.child2])) + self.assertEquals(set(InheritParent.objects.all().cast()), + set([parent, self.child, self.child2])) class TimeStampedModelTests(TestCase):