Skip to main content

Sane single table model inheritance for Django

Project description

https://travis-ci.org/craigds/django-typed-models.png?branch=master https://coveralls.io/repos/craigds/django-typed-models/badge.png?branch=master

Intro

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.

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):
    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:

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

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.

Requirements

  • Python 2.6, 2.7, 3.3

  • Django 1.5+

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-typed-models-0.3.0.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file django-typed-models-0.3.0.tar.gz.

File metadata

File hashes

Hashes for django-typed-models-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7ae3f2b01558945a4ae279325039d0329b93a849202569ee01a116ad0285329a
MD5 6bb6f43ae1cfee37de0d5b13f12ce9a4
BLAKE2b-256 de73c1c88f4a973965bc594f359e2f145834c648c853115de507c5d56d85e805

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page