TypeError: __init__() missing 1 required positional argument: 'on_delete'

Posted pengweiblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: __init__() missing 1 required positional argument: 'on_delete'相关的知识,希望对你有一定的参考价值。

在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: ‘on_delete‘

 

on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五个可选择的值
CASCADE:此值设置,是级联删除。
PROTECT:此值设置,是会报完整性错误。
SET_NULL:此值设置,会把外键设置为null,前提是允许为null。
SET_DEFAULT:此值设置,会把设置为外键的默认值。
SET():此值设置,会调用外面的值,可以是一个函数。
一般情况下使用CASCADE就可以了。

以上是关于TypeError: __init__() missing 1 required positional argument: 'on_delete'的主要内容,如果未能解决你的问题,请参考以下文章