我在应用程序从yaml描述文件启动时动态生成django模型。我在django admin中注册它们,但它们在管理界面中显示为禁用。看起来像this。
我创建了manage.py命令,以yaml格式从描述文件中动态创建模型。
from django.core.management.base import BaseCommand
from django.core.management import call_command
import yaml
from generation.generate import generate_fields, generate_model
class Command(BaseCommand):
help = 'Generates or updates django models from specified file'
def handle(self, *args, **options):
try:
f = open(args[0], 'r')
models = yaml.safe_load(f)
f.close()
for name, definition in models.items():
fields = generate_fields(definition['fields'])
generate_model(name, definition['title'], fields, "generation", "generation.models")
call_command('syncdb', interactive=True)
except IOError:
self.stderr.write('Cannot open file: {0}'.format(args[0]))
except yaml.YAMLError as e:
self.stderr.write('Cannot parse file {0}: {1}'.format(args[0], e))
我从root urls.py启动此命令,如下所示:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from generation import views
from generation.management.commands.updatemodels import Command
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', views.main, name='main'),
# url(r'^modgen/', include('modgen.foo.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Command().handle('models.yaml')
我以这种方式生成模型并在管理员中注册:
from sys import stderr
from django.contrib import admin
from django.db import models
class Types():
INT = 'int'
CHAR = 'char'
DATE = 'date'
def generate_model(model_name, model_title, fields, app_label, module):
class Meta:
pass
if app_label:
setattr(Meta, 'app_label', app_label)
if model_title:
setattr(Meta, 'verbose_name_plural', model_title)
attrs = {'__module__': module, 'Meta': Meta}
if fields is not None:
attrs = dict(attrs.items() + fields.items())
model = type(model_name, (models.Model,), attrs)
admin.site.register(model)
def generate_fields(definition):
fields = {}
for field in definition:
fields[field['id']] = generate_field(field['title'], field['type'])
return fields
def generate_field(title, field_type):
if field_type == Types.CHAR:
return models.CharField(title, max_length=255)
elif field_type == Types.INT:
return models.IntegerField(title)
elif field_type == Types.DATE:
return models.DateField(title)
else:
stderr.write('Unknown field type specified: {0}'.format(field_type))
为什么模型在管理界面中显示为禁用? Django成功地注册了它们。
答案 0 :(得分:0)
可能首先需要编辑(或创建)文件generation/admin.py
,然后添加此代码以在django-admin部分注册应用程序模型。
from django.contrib import admin
from myproject.generation.models import Rooms, Users
class RoomsAdmin(admin.ModelAdmin):
pass
class UsersAdmin(admin.ModelAdmin):
pass
admin.site.register(Rooms, RoomsAdmin)
admin.site.register(Users, UsersAdmin)