Skip to main content

Sane single table model inheritance for Django

Reason this release was yanked:

broken

Project description

django-typed-models

example workflow

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 actual type of each object is stored in the database, and when the object is retrieved it is automatically cast to the correct model class.

Licensed under the New BSD License.

Features

  • Models in querysets have the right class automatically
  • 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.models 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()

Listing subclasses

Occasionally you might need to list the various subclasses of your abstract type.

One current use for this is connecting signals, since currently they don't fire on the base class (see #1)

    for sender in Animal.get_type_classes():
        post_save.connect(on_animal_saved, sender=sender)

Django admin

If you plan to use typed models with Django admin, consider inheriting from typedmodels.admin.TypedModelAdmin. This will hide the type field from subclasses admin by default, and allow to create new instances from the base class admin.

from django.contrib import admin
from typedmodels.admin import TypedModelAdmin
from .models import Animal, Canine, Feline

@admin.register(Animal)
class AnimalAdmin(TypedModelAdmin):
    pass

@admin.register(Canine)
class CanineAdmin(TypedModelAdmin):
    pass

@admin.register(Feline)
class FelineAdmin(TypedModelAdmin):
    pass

Limitations

  • Since all objects are stored in the same table, all fields defined in subclasses are nullable.
  • Fields defined on subclasses can only be defined on one subclass, unless the duplicate fields are exactly identical.

Requirements

  • a non-EOL combination of Django and Python

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.16.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_typed_models-0.16.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file django_typed_models-0.16.1.tar.gz.

File metadata

  • Download URL: django_typed_models-0.16.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_typed_models-0.16.1.tar.gz
Algorithm Hash digest
SHA256 f145f8cb6748623158f065e9d746f670852deb8e8e416061e950f48057471457
MD5 8290288156aa30344b1ef0c85388a992
BLAKE2b-256 c800a98e65821a9d90b493cb84a8b78cbe84bdfcf1d3908b04db46953cb6cd3c

See more details on using hashes here.

File details

Details for the file django_typed_models-0.16.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_typed_models-0.16.1-py3-none-any.whl
Algorithm Hash digest
SHA256 229818016bab15e9cba39a39f180b5293c1145d15ef67318499233abef8e20e7
MD5 e4a1b1909cb6b2c2d94f93c948038748
BLAKE2b-256 8afc6cceabfda55d6c7a3da4788f195f8dffaa7f682cf01b3ccf269a6aeb875e

See more details on using hashes here.

Supported by

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