Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime model generation using django

I have an application that needs to generate its models on runtime.
This will be done according to the current database scheme.
How can it be done?
How can I create classes on runtime in python?
Should I create a json representation and save it in a database and then unserialize it into a python object?

like image 767
the_drow Avatar asked Dec 22 '22 22:12

the_drow


1 Answers

You can try to read this http://code.djangoproject.com/wiki/DynamicModels

Here is example how to create python model class:

Person = type('Person', (models.Model,), {
    'first_name': models.CharField(max_length=255),
    'last_name': models.CharField(max_length=255),
})

You can also read about python meta classes:
- What is a metaclass in Python?
- http://www.ibm.com/developerworks/linux/library/l-pymeta.html
- http://gnosis.cx/publish/programming/metaclass_1.html

like image 54
Dominik Szopa Avatar answered Dec 24 '22 13:12

Dominik Szopa