Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
polymorphic django models using automatic type-field downcasting
branch: master

This branch is 75 commits behind craigds:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
tests
typedmodels
.gitignore
LICENSE.txt
README.rst
setup.py

README.rst

django-typed-models

Intro

django-typed-models provides an extra type of model inheritance for django.

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. These are basically proxy models, but with a known type.

Licensed under the New BSD License.

Features

  • 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

Usage:

An example says a bunch of words:

# myapp/models.py

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__, self.name)

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

class Feline(Animal):
    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:

Feline.objects.update(type='myapp.bigcat')

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

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

Limitations

These are just special proxy models, so all the objects are stored in the same table. As a result, subclasses can't have new fields defined. If you need to do that, you should just use django's built-in multiple-table inheritance.

Requirements

  • Python 2.6+ (not py3 compatible yet)
  • Django 1.4+
Something went wrong with that request. Please try again.