在运行 python manage.py makeigrations app_name时,若调用models.ForeignKey时参数里除了对应的model没有其他参数时,会报“TypeError: __init__() missing 1 required positional argument: 'on_delete'”的错误。
究其原因,发现在virt_env\Lib\site-packages\django\db\models\fields\related.py的class ForeignKey(ForeignObject)中,有如下check.
def _check_on_delete(self):
on_delete = getattr(self.remote_field, 'on_delete', None)
if on_delete == SET_NULL and not self.null:
return [
checks.Error(
'Field specifies on_delete=SET_NULL, but cannot be null.',
hint='Set null=True argument on the field, or change the on_delete rule.',
obj=self,
id='fields.E320',
)
]
elif on_delete == SET_DEFAULT and not self.has_default():
return [
checks.Error(
'Field specifies on_delete=SET_DEFAULT, but has no default value.',
hint='Set a default value, or change the on_delete rule.',
obj=self,
id='fields.E321',
)
]
else:
return []解决方法之一:在参数里加上如下参数“,on_delete=CASCADE,”或者“,on_delete=DO_NOTHING,"。
在执行python manage.py makeigrations app_name时遇到'TypeError: __init__() missing 1 required positional argument: 'on_delete''错误。问题源于Django的ForeignKey字段必须包含'on_delete'参数。解决方案是在定义外键时指定'on_delete'行为,如models.ForeignKey(OtherModel, on_delete=models.CASCADE)。"
138719416,16848851,"C++编程技巧:explicit, 静态成员与常量成员,vector与array对比
996

被折叠的 条评论
为什么被折叠?



