Anonymous avatar Anonymous committed b6e99b0

docs

Comments (0)

Files changed (2)

 
 Django model mixins and utilities.
 
+InheritanceCastModel
+====================
+
+This abstract base class can be inherited by the root (parent) model
+in a model-inheritance tree.  It allows each model in the tree to
+"know" what type it is (via an automatically-set foreign key to
+``ContentType``), allowing for automatic casting of a parent instance
+to its proper leaf (child) type.
+
+For instance, if you have a ``Place`` model with subclasses
+``Restaurant`` and ``Bar``, you may want to query all Places::
+
+    nearby_places = Place.objects.filter(location='here')
+
+But when you iterate over ``nearby_places``, you'll get only ``Place``
+instances back, even for objects that are "really" ``Restaurant`` or
+``Bar``.  If you have ``Place`` inherit from ``InheritanceCastModel``,
+you can just call the ``cast()`` method on each ``Place`` and it will
+return an instance of the proper subtype, ``Restaurant`` or ``Bar``.
+
+.. note:: 
+    This is inefficient for large querysets, as it results in n
+    queries to the subtype tables.  It would be possible to write a
+    QuerySet subclass that could reduce this to k queries, where there
+    are k subtypes in the inheritance tree.
 TODO list for django-model-utils
 ================================
 
+* Custom QuerySet subclass to pair with InheritanceCastModel for more
+  efficient querying.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.