missing 1 required positional argument on_delete --Django2.0

Django2.0+
使用ForeignKey 报错
TypeError: __init__() missing 1 required positional argument: 'on_delete'

解决办法:
from django.db import models
class Article(models.Model):
category = models.ForeignKey('Category', on_delete=models.PROTECT)
title = models.CharField(max_length=55)
# ...

def __str__(self):
return self.title

定义ForeignKey的时候添加参数on_delete,然后需要重新migration model,不然还是会报错。
(如果还是不行,把migration文件夹的内容全部删掉,在migrated一下)

0 个评论

要回复文章请先登录注册