迁移与 forms.py 冲突 [英] Migration clashes with forms.py

查看:28
本文介绍了迁移与 forms.py 冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令 python manage.py makemigrations 由于 forms.py 而失败,其中在类定义级别引用了新模型或新字段.

The command python manage.py makemigrations fails most of time due to the forms.py, in which new models or new fields are referenced at class definition level.

所以我必须评论每个这样的定义,以便迁移操作.这是一项痛苦的任务.

So I have to comment each such definitions for the migration to operate. It's a painfull task.

我不明白为什么迁移过程会导入 forms.py 模块.我认为导入模型模块应该就足够了.

I don't understand why the migration process import the forms.py module. I think that importing models modules should be sufficient.

有没有办法避免这些错误?

Is there a way to avoid those errors ?

推荐答案

感谢@alasdair 我理解了我的问题并找到了解决方法:我替换了 views.py 文件中的原始代码>

Thanks to @alasdair I understood my problem and found a workaround: I replace the original code in the views.py file

from MyApp import forms

import sys
if 'makemigrations' not in sys.argv and 'migrate' not in sys.argv:
    from MyApp import forms

在我的情况下它工作正常,但我想有更好的方法来了解当前进程是否是迁移.如果是这样,请指教.

It works fine in my case, but I suppose there is a better way to know if the current process is a migration or not. If so, please advise.

这篇关于迁移与 forms.py 冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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