我使用Django 1.11,我试图通过引用此链接https://code.djangoproject.com/wiki/DynamicModels创建Django动态模型,通过执行它没有任何问题运行的每一步,但是当我看到注册模型在Django管理面板中,模型在它下面对应的应用程序中可见,但它不可访问,我不知道我错过了什么。Django动态模型,无法在管理中访问
def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None):
"""
Create specified model
"""
class Meta:
# Using type('Meta', ...) gives a dictproxy error during model creation
pass
if app_label:
# app_label must be set using the Meta inner class
setattr(Meta, 'app_label', app_label)
# Update Meta with any options that were provided
if options is not None:
for key, value in options.iteritems():
setattr(Meta, key, value)
# Set up a dictionary to simulate declarations within a class
attrs = {'__module__': module, 'Meta': Meta}
# Add in any fields that were provided
if fields:
attrs.update(fields)
# Create the class, which automatically triggers ModelBase processing
model = type(name, (models.Model,), attrs)
# Create an Admin class if admin options were provided
if admin_opts is not None:
class Admin(admin.ModelAdmin):
pass
for key, value in admin_opts:
setattr(Admin, key, value)
admin.site.register(model, Admin)
return model
请参考参考这一形象:
这里模型测试11由动态建模方法创建的,我不能够访问模型,添加/更改按钮也不存在,当我杀死服务器并重新启动它时,该动态创建的模型会隐藏起来。
如何访问此模型?我错过了这里的任何步骤或者其中有什么问题?
据我所知,当Django应用程序启动时,它缓存所有ModelAdmin操作。在创建新模型时,您需要跟踪django应用程序初始化逻辑并缓存或重新启动Django应用程序。 – vadimchin