Sane single table model inheritance for Django
Project description
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.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()
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.8+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-typed-models-0.6.0.tar.gz
.
File metadata
- Download URL: django-typed-models-0.6.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5325cf75c8ddb79207912c89b16c9bbbd7f5265b5eb77a942983b0f85b77ece |
|
MD5 | ff4a47360db5483f81fa39319ae0a905 |
|
BLAKE2b-256 | 27e04da92859fe4cb445c919d52bf52cf7c08fa540880f6a666f675812e07fff |
Provenance
File details
Details for the file django_typed_models-0.6.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_typed_models-0.6.0-py2.py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c86d718b58241eb239a7e8a8f2d7b5937ff217a77dfe6a3aa6e4af2e7c90978 |
|
MD5 | 5a984caf9b9b5b2c8c115511ae8dd34e |
|
BLAKE2b-256 | e5ca2f79a185f762c8a943e30f8fbef56230a4585b0f6c26e2da6d4446856d2e |