在将Django项目移植到Python 3和Django 2时迁移问题 [英] Migrating problems when porting Django project to Python 3 and Django 2
问题描述
我一直在将Django项目移植到Python 3和Django2。我必须按照Django 2的要求,使用外键将on_delete添加到我的所有模型中。现在,我已尝试进行这些更改的迁移 TypeError:__init __()缺少1个必需的位置参数:'on_delete'
。
I've been porting a Django project to Python 3 and Django 2. I have had to add on_delete to all my models with foreign keys as required in Django 2. Now I have tried to make migrations for those changes have been getting TypeError: __init__() missing 1 required positional argument: 'on_delete'
.
其引用的文件为0002迁移文件,而不是已更新的模型文件。我不确定如何解决此问题。我尝试伪造迁移,但仍然遇到相同的错误。
The file it references is the 0002 migration file not the models file which has been updated. I am not sure how to go about fixing this. I have tried faking the migrations and I still get the same error.
我不确定为什么它认为数据库不存在,我已经检查过并且一切都完好无损在Postgres工作。有想法吗?
I am not sure why it thinks the database doesn't exist, I have checked and everything is intact and working in Postgres. Any ideas?
推荐答案
因为 django-2.0 ForeignKey
字段[Django-doc] 和 OneToOneField
字段字段现在具有必需的 on_delete
参数。
Since django-2.0 ForeignKey
fields [Django-doc] and OneToOneField
fields fields now have a required on_delete
parameter.
在在 2.0中删除的功能下的Django-2.0发行说明 :
ForeignKey $的
on_delete
参数在模型和迁移中,现在需要是c $ c>和OneToOneField
。考虑压缩迁移,以便您减少更新。
The
on_delete
argument forForeignKey
andOneToOneField
is now required in models and migrations. Consider squashing migrations so that you have fewer of them to update.
因此,您应该检查迁移文件中的 ForeignKey
s和 OneToOneField
s,并添加 on_delete
参数,例如:
You thus should inspect your migration files for ForeignKey
s and OneToOneField
s, and add an on_delete
parameter, like:
class Migration(migrations.Migration):
initial = False
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Model',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('some_foreignkey', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.OtherModel')),
],
),
]
您应检查关于 on_delete
参数的文档以查看哪种删除策略最适合每种情况在撰写本文时,这些选项包括 CASCADE
, PROTECT
, SET_NULL
, SET_DEFAULT
, SET(..)
,不要做什么
。
You should inspect the documentation on the on_delete
parameter to see what deletion strategy is the best for each situation. The options are, at the time of writing CASCADE
, PROTECT
, SET_NULL
, SET_DEFAULT
, SET(..)
, DO_NOTHING
.
如果您未在 CASCADE
。因此,如果您想要相同的行为,则应添加 on_delete = models.CASCADE
。在 1.11版本中对此进行了说明 on_delete
上的文档:
If you did not specify the on_delete
in the pre-django-2.0 versions, it made a default to CASCADE
. So if you want the same behavior, you should add on_delete=models.CASCADE
. This is noted in the 1.11 version of the documentation on on_delete
:
从1.9版开始不推荐使用: on_delete将成为Django 2.0中的必需参数。在旧版本中,默认值为
CASCADE
。
Deprecated since version 1.9: on_delete will become a required argument in Django 2.0. In older versions it defaults to
CASCADE
.
这篇关于在将Django项目移植到Python 3和Django 2时迁移问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!