Skip to main content
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.7 or 3.3+

  • Django 1.7+

Release files for django-typed-models 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-typed-models 0.5.0
File Size Uploaded
django-typed-models-0.5.0.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-typed-models 0.5.0
File Interpreter ABI Platform
django_typed_models-0.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.9 kB

Release files / django-typed-models-0.5.0.tar.gz

Download URL django-typed-models-0.5.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ea2a1bc49cf6b9506f3820d1639ebf614314b1a6400b77d4fc08eff0b4c1cee0
BLAKE2b-256 checksum
How to use checksums
58f1b4a5b6057711a286782b044b3f189ef198b7cd862fc01f4ad803e2d00916
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_typed_models-0.5.0-py2.py3-none-any.whl

Download URL django_typed_models-0.5.0-py2.py3-none-any.whl
Size 10.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
56cf6f3e5f94518d7d91e5feebfc662961f84188c0a0f7e877d9f2b63ea62678
BLAKE2b-256 checksum
How to use checksums
8af253e8117a1f2421b4bf49c5fca196ee4f682a116ac7633f1a88d17542e0d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.16.2

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.2

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

This release

0.5.0 This release

2 release files

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page