Code Monkey home page Code Monkey logo

django-typed-models's Introduction

django-typed-models

https://travis-ci.org/craigds/django-typed-models.svg?branch=master https://coveralls.io/repos/craigds/django-typed-models/badge.svg?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 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)

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.

Requirements

django-typed-models's People

Contributors

bogdanstaicu avatar craigds avatar krzysiekj avatar magopian avatar sloria avatar sobolevn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.