coding utf-8 import os import yaml from django conf import settings fr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding:utf-8 -*-
import os
import yaml
from django.conf import settings
from django.contrib import admin
from django.db import models
fields = {
'int': models.IntegerField,
'char': models.CharField,
}
#if os.path.exists(settings.MODELS_YAML_PATH):
models_file = open(settings.MODELS_YAML_PATH, 'r')
models_yaml = yaml.load(models_file)
for k, v in models_yaml.iteritems():
attrs = {'__module__': 'vaal_test.test_app.models',
'__unicode__': lambda self: str(self.id),}
for field in v['fields']:
if 'max_length' in field:
new_field = fields[field['type']](verbose_name=field['title'], max_length=field['max_length'])
else:
new_field = fields[field['type']](verbose_name=field['title'],)
attrs[field['id']] = new_field
new_model = type(k, (models.Model,), attrs)
new_model._meta.verbose_name_plural= k
admin.site.register(new_model)