Created
March 5, 2011 16:51
-
-
Save runekaagaard/856498 to your computer and use it in GitHub Desktop.
The other revision apps for django don't work well enough. NIH!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models as m | |
from tesla.models import Invention | |
class _DUMMY(object): | |
pass | |
def get_model_dict(model): | |
def get_foreign_key_key(model): | |
return '%s_%s' % ( | |
model.__module__.replace('.models', '').replace('.', '_'), | |
model.__class__.__name__.lower(), | |
) | |
def get_foreign_key_name(model): | |
return '%s.%s' % ( | |
model.__module__.replace('.models', ''), | |
model.__class__.__name__ | |
) | |
modeldict = { | |
'__module__': _DUMMY.__module__ , | |
get_foreign_key_key(model): m.ForeignKey(get_foreign_key_name(model)), | |
} | |
for name in model._meta.get_all_field_names(): | |
field_object, _model, direct, m2m = model._meta.get_field_by_name(name) | |
modeldict[name] = field_object | |
return modeldict | |
def get_history_model_name(model): | |
return '%s%s' % ( | |
"".join([p.capitalize() for p in model.__module__.split('.') if p != 'models']), | |
model.__class__.__name__.capitalize() | |
) | |
def build_class(name, bases, _dict): | |
globals()[name] = type(name, bases, _dict) | |
model = Invention() | |
build_class(get_history_model_name(model), (m.Model, ), get_model_dict(model)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment