在运行 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_de