Simple helper to extend django models using mixins
Python
Switch branches/tags
Nothing to show
Latest commit d95d9cd Sep 20, 2015 @tbartelmess Merge pull request #9 from kekomal/patch-1
Support also model managers in the mixin
Permalink
Failed to load latest commit information.
modelmixins Support also model managers in the mixin Sep 16, 2015
.gitignore Updated gitignore Dec 8, 2012
README.md Update README.md Dec 9, 2012
setup.py Bump version Mar 5, 2014

README.md

django-model-mixins

Simple helper to extend django models using mixins

When you mixin a django model into another one, it is probably behaving not how you want it to. This is caused by django's metaclass magic.

This package provides a single class ModelMixin that you can use as the base for mixins, that provide additional fields to the object the class get's mixed in to.

ModelMixin is a direct subclass of object and therefore it's subclass is not going to be an own table.

Limitations

  • No meta options on mixin model

Example

Model

from django.db import models
from modelmixins import ModelMixin

class Car(models.Model):
    model = models.CharField(max_length=200)
    color = models.CharField(max_length=200)

class CarOwner(ModelMixin):
    cars = models.ManyToManyField(Car)


class Person(models.Model, CarOwner):
    firstname = models.CharField(max_length=200)
    lastname = models.CharField(max_length=200)

SQL Output

BEGIN;
CREATE TABLE "cars_car" (
    "id" integer NOT NULL PRIMARY KEY,
    "model" varchar(200) NOT NULL,
    "color" varchar(200) NOT NULL
)
;
CREATE TABLE "cars_person_cars" (
    "id" integer NOT NULL PRIMARY KEY,
    "person_id" integer NOT NULL,
    "car_id" integer NOT NULL REFERENCES "cars_car" ("id"),
    UNIQUE ("person_id", "car_id")
)
;
CREATE TABLE "cars_person" (
    "id" integer NOT NULL PRIMARY KEY,
    "firstname" varchar(200) NOT NULL,
    "lastname" varchar(200) NOT NULL
)
;
COMMIT;