Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit 2ddb9baaa411d6555a4759cb2e0a8c71a88df42e 1 parent b24f374
Thomas Bartelmess authored

Showing 2 changed files with 66 additions and 0 deletions. Show diff stats Hide diff stats

  1. +40 0 modelmixins/__init__.py
  2. +26 0 setup.py
40 modelmixins/__init__.py
... ... @@ -0,0 +1,40 @@
  1 +"""
  2 +Mixin support for django models
  3 +"""
  4 +from django.db import models
  5 +from django.db.models.signals import class_prepared
  6 +from django.dispatch import receiver
  7 +from django.db.models.fields import Field
  8 +
  9 +
  10 +class ModelMixin(object):
  11 + """
  12 + Base class for model mixins
  13 + """
  14 +
  15 + @classmethod
  16 + def model_mixin(cls):
  17 + """
  18 + Adds the fields of the class to the all classes that mix it in
  19 + """
  20 + fields = {}
  21 +
  22 + for (name, attr) in cls.__dict__.items():
  23 + if isinstance(attr, models.Field):
  24 + fields[name] = attr
  25 +
  26 + for fieldname in fields.keys():
  27 + delattr(cls, fieldname)
  28 +
  29 + for subclass in cls.__subclasses__():
  30 + assert issubclass(subclass, models.Model)
  31 + for (fieldname, field) in fields.items():
  32 + field.contribute_to_class(subclass, fieldname)
  33 +
  34 +
  35 +
  36 +@receiver(class_prepared)
  37 +def mixin(sender, **kwargs):
  38 + for base in sender.__bases__:
  39 + if issubclass(base, ModelMixin):
  40 + base.model_mixin()
26 setup.py
... ... @@ -0,0 +1,26 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +"""
  4 +Setup script
  5 +"""
  6 +
  7 +from distutils.core import setup
  8 +
  9 +setup(name='django-model-mixins',
  10 + version='1.0',
  11 + description='Simple helper to extend django models using mixins',
  12 + author='Thomas Bartelmess',
  13 + author_email='thomas.bartelmess@me.com',
  14 + url='https://github.com/tbartelmess/django-model-mixins',
  15 + packages=['modelmixins'],
  16 + classifiers=[
  17 + 'Development Status :: 3 - Alpha',
  18 + 'Environment :: Web Environment',
  19 + 'Framework :: Django',
  20 + 'Intended Audience :: Developers',
  21 + 'License :: OSI Approved :: BSD License',
  22 + 'Operating System :: OS Independent',
  23 + 'Programming Language :: Python',
  24 + 'Topic :: Utilities'
  25 + ],
  26 + )

0 comments on commit 2ddb9ba

Please sign in to comment.
Something went wrong with that request. Please try again.