1. Anton Strogonoff
  2. django-model-utils


Carl Meyer  committed 7ece844 Merge


  • Participants
  • Parent commits 7bf29f1, 37887e8
  • Branches default

Comments (0)

Files changed (5)

File .gitignore

View file
  • Ignore whitespace

File CHANGES.rst

View file
  • Ignore whitespace
 tip (unreleased)
+- Added ``InheritanceManager.get_subclass()``. Thanks smacker.
 1.0.0 (2011.06.16)

File README.rst

View file
  • Ignore whitespace
     nearby_places = Place.objects.select_subclasses("restaurant")
     # restaurants will be Restaurant instances, bars will still be Place instances
-If you don't explicitly call ``select_subclasses()``, an ``InheritanceManager``
-behaves identically to a normal ``Manager``; so it's safe to use as your
-default manager for the model.
+``InheritanceManager`` also provides a subclass-fetching alternative to the
+``get()`` method::
+    place = Place.objects.get_subclass(id=some_id)
+    # "place" will automatically be an instance of Place, Restaurant, or Bar
+If you don't explicitly call ``select_subclasses()`` or ``get_subclass()``,
+an ``InheritanceManager`` behaves identically to a normal ``Manager``; so
+it's safe to use as your default manager for the model.
 .. note::
     ``InheritanceManager`` currently only supports a single level of model

File model_utils/managers.py

View file
  • Ignore whitespace
     def select_subclasses(self, *subclasses):
         return self.get_query_set().select_subclasses(*subclasses)
+    def get_subclass(self, *args, **kwargs):
+        return self.get_query_set().select_subclasses().get(*args, **kwargs)
 class InheritanceCastMixin(object):
     def cast(self):

File model_utils/tests/tests.py

View file
  • Ignore whitespace
+        def test_get_subclass(self):
+            self.assertEquals(
+                self.get_manager().get_subclass(pk=self.child1.pk),
+                self.child1)
     class InheritanceManagerRelatedTests(InheritanceManagerTests):
         def setUp(self):