Django:如何迁移在运行时制作的动态模型 [英] Django: How to migrate dynamic models made at runtime

查看:91
本文介绍了Django:如何迁移在运行时制作的动态模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Django应用中,特定的用户输入将导致创建新模型。这是我用来创建模型并注册它的代码。

In my Django app, a specific user input will result in the creation of a new model. Here is the code I am using to create the model and register it.

model = type(model_name, (ExistingModel,), attrs)
admin.site.register(model, admin_options)

from django.core.urlresolvers import clear_url_caches
from django.utils.module_loading import import_module
reload(import_module(settings.ROOT_URLCONF))
clear_url_caches()

此操作成功创建了新模型,但是,当我单击模型以查看管理页面上的表时,出现以下错误:

This successfully creates the new model, however, when I click on the model to see the table on the admin page, I get the following error:


relation ExistingModel_NewModel确实不存在

relation "ExistingModel_NewModel" does not exist

这通常意味着新模型更改尚未迁移。如何在Django中迁移动态创建的模型以查看其相应的数据表?

This usually means that the new model changes have not been migrated. How can I migrate dynamically created models in Django to see their corresponding data tables?

推荐答案

一个简单的解决方案对我有用。在创建动态模型之后,我最终像这样运行了 makemigrations migrate 管理命令:

A simple solution worked for me. I ended up running the makemigrations and migrate management commands like so, after creating the dynamic model:

from django.core.management import call_command
call_command('makemigrations')
call_command('migrate')

这篇关于Django:如何迁移在运行时制作的动态模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆