additional test, docs cleanup

This commit is contained in:
Carl Meyer 2010-10-05 08:59:25 -04:00
parent c5caaadcaf
commit a2c67934ed
4 changed files with 25 additions and 20 deletions

View file

@ -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)
------------------

View file

@ -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
================

View file

@ -12,6 +12,9 @@ class InheritParent(InheritanceCastModel):
class InheritChild(InheritParent):
pass
class InheritChild2(InheritParent):
pass
class TimeStamp(TimeStampedModel):
pass

View file

@ -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):