将 Django 项目移植到 Python 3 和 Django 2 时的迁移问题
Migrating problems when porting Django project to Python 3 and Django 2
我一直在将一个 Django 项目移植到 Python 3 和 Django 2。我必须按照 Django 2 的要求将 on_delete 添加到我所有的带有外键的模型中。现在我有尝试对这些更改进行迁移 TypeError: __init__() missing 1 required positional argument: 'on_delete'
。
它引用的文件是0002迁移文件,不是已经更新的模型文件。我不确定如何解决这个问题。我试过伪造迁移,但我仍然遇到同样的错误。
我不确定为什么它认为数据库不存在,我已经检查过并且一切都完好无损并且在 Postgres 中工作。有什么想法吗?
因为 django-2.0 ForeignKey
fields [Django-doc] and OneToOneField
fields fields now have a required on_delete
parameter.
这是在release notes of Django-2.0 under Features removed in 2.0中指定的:
The on_delete
argument for ForeignKey
and OneToOneField
is now required in models and migrations. Consider squashing migrations so that you have fewer of them to update.
因此,您应该检查迁移文件中的 ForeignKey
和 OneToOneField
,并添加一个 on_delete
参数,例如:
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(<b>on_delete=django.db.models.deletion.CASCADE</b>, to='app.OtherModel')),
],
),
]
您应该检查 documentation on the on_delete
parameter 以了解哪种删除策略最适合每种情况。在撰写本文时,选项为 CASCADE
、PROTECT
、SET_NULL
、SET_DEFAULT
、SET(..)
、DO_NOTHING
、
如果你没有在前面指定on_delete
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
:
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。我必须按照 Django 2 的要求将 on_delete 添加到我所有的带有外键的模型中。现在我有尝试对这些更改进行迁移 TypeError: __init__() missing 1 required positional argument: 'on_delete'
。
它引用的文件是0002迁移文件,不是已经更新的模型文件。我不确定如何解决这个问题。我试过伪造迁移,但我仍然遇到同样的错误。
我不确定为什么它认为数据库不存在,我已经检查过并且一切都完好无损并且在 Postgres 中工作。有什么想法吗?
因为 django-2.0 ForeignKey
fields [Django-doc] and OneToOneField
fields fields now have a required on_delete
parameter.
这是在release notes of Django-2.0 under Features removed in 2.0中指定的:
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
和 OneToOneField
,并添加一个 on_delete
参数,例如:
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(<b>on_delete=django.db.models.deletion.CASCADE</b>, to='app.OtherModel')),
],
),
]
您应该检查 documentation on the on_delete
parameter 以了解哪种删除策略最适合每种情况。在撰写本文时,选项为 CASCADE
、PROTECT
、SET_NULL
、SET_DEFAULT
、SET(..)
、DO_NOTHING
、
如果你没有在前面指定on_delete
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
:
Deprecated since version 1.9: on_delete will become a required argument in Django 2.0. In older versions it defaults to
CASCADE
.