skip to navigation
skip to content

django-typed-models 0.5.0

Sane single table model inheritance for Django


django-typed-models provides an extra type of model inheritance for Django. It is similar to single-table inheritance in Ruby on Rails.

The concrete type of each object is stored in the database, and when the object is retrieved it is automatically cast to the correct concrete type.

Licensed under the New BSD License.


  • Automatic downcasting of models from querysets
  • All models subclassing a common base are stored in the same table
  • object types are stored in a ‘type’ field in the database
  • No extra queries or joins to retrieve multiple types


An example says a bunch of words:

# myapp/

from django.db import models
from typedmodels import TypedModel

class Animal(TypedModel):
    Abstract model
    name = models.CharField(max_length=255)

    def say_something(self):
        raise NotImplemented

    def __repr__(self):
        return u'<%s: %s>' % (self.__class__.__name__,

class Canine(Animal):
    def say_something(self):
        return "woof"

class Feline(Animal):
    mice_eaten = models.IntegerField(
        default = 0

    def say_something(self):
        return "meoww"
# later
 >>> from myapp.models import Animal, Canine, Feline
 >>> Feline.objects.create(name="kitteh")
 >>> Feline.objects.create(name="cheetah")
 >>> Canine.objects.create(name="fido")
 >>> print Animal.objects.all()
 [<Feline: kitteh>, <Feline: cheetah>, <Canine: fido>]

 >>> print Canine.objects.all()
 [<Canine: fido>]

 >>> print Feline.objects.all()
 [<Feline: kitteh>, <Feline: cheetah>]

You can actually change the types of objects. Simply run an update query:


If you want to change the type of an object without refreshing it from the database, you can call recast:

# or kitty.recast('myapp.bigcat')


Since all objects are stored in the same table, all fields defined in subclasses are nullable.

Known issues

  • Error in South migration when m2m field related to model not inheriting directly from TypedModel is used.
  • XML serialization doesn’t work.


  • Python 2.7 or 3.3+
  • Django 1.7+
File Type Py Version Uploaded on Size
django-typed-models-0.5.0.tar.gz (md5) Source 2015-10-09 9KB
django_typed_models-0.5.0-py2.py3-none-any.whl (md5) Python Wheel 2.7 2015-10-09 10KB
  • Downloads (All Versions):
  • 42 downloads in the last day
  • 464 downloads in the last week
  • 1661 downloads in the last month