django-model-utils 1.5.0

Django model mixins and utilities
The django-model-utils package contains Django model mixins and utilities.


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:

from model_utils.models import InheritanceCastModel

class Place(InheritanceCastModel):

class Restaurant(Place):

nearby_places = Place.objects.filter(location='here')
for place in nearby_places:
 restaurant_or_bar = place.cast()


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.


This abstract base class just provides self-updating created and modified fields on any model that inherits it.

Please report bugs to the official home page:

last updated on:
April 18th, 2012, 21:22 GMT
license type:
BSD License 
developed by:
Carl Meyer
ROOT \ Internet \ Django Plugins
Download Button

In a hurry? Add it to your Download Basket!

user rating



Rate it!
What's New in This Release:
  • Choices now accepts option-groupings. Fixes GH-14.
  • Choices can now be added to other Choices or to any iterable, and can be compared for equality with itself. Thanks Tony Aldridge. (Merge of GH-76.)
  • Choices now __contains__ its Python identifier values. Thanks Keryn Knight. (Merge of GH-69).
  • Fixed a bug causing KeyError when saving with the parameter update_fields in which there are untracked fields. Thanks Mikhail Silonov. (Merge of GH-70, fixes GH-71).
read full changelog

Add your review!