1.django从1.9开始ForeignKey中的on_delete参数是必须的。
2.案例
代码:
from django.db import models
class Topic(models.Model):
"""用户学习主题"""
text = models.CharField(max_length=200)
data_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""返回模型的字符串表示"""
return self.text
class Entry(models.Model):
"""学到的有关某个主题的具体知识"""
topic = models.ForeignKey(Topic)
text = models.TextField()
data_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""返回模型的字符串表示"""return self.text[:50] + "..."
结果:
解决方案:
将foreignkey的on_delete属性设置为models.CASCADE,即将上面
topic = models.ForeignKey(Topic)改为topic = models.ForeignKey(Topic,on_delete=models.CASCADE)