迁移与forms.py冲突 [英] Migration clashes with forms.py
问题描述
由于 forms.py
,命令 python manage.py makemigrations
大部分时间失败,其中新增模型或新字段在类定义级别引用。
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屋!