
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
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.
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()
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)
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
FAQs
Sane single table model inheritance for Django
We found that django-typed-models demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.