Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

 代码:

from django.db import models

class Test(models.Model):
    name = models.CharField(max_length=20)

class Contact(models.Model):
    name = models.CharField(max_length=200)
    age = models.IntegerField(default=0)
    email = models.EmailField()
    def __unicode__(self):
        return self.name

class Tag(models.Model):
    contact = models.ForeignKey(Contact)
    name = models.CharField(max_length=50)
    def __unicode__(self):
        return self.name

报错:

解决办法:

将第十四行的代码改为:

contact = models.ForeignKey('Contact', on_delete=models.CASCADE,)
即在外键值的后面加上 on_delete=models.CASCADE
posted on 2018-05-04 16:39    阅读(419)  评论(0编辑  收藏  举报