Django2 外键遇到的坑

# 出版社
class Publisher(models.Model):
    # 自增、主键 id
    id = models.AutoField(primary_key=True)
    # varchar(32)、不为空、唯一索引
    name = models.CharField(max_length=32, null=False, unique=True)


#
class Booker(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, null=False, unique=True)
    publisher = models.ForeignKey(to="Publisher")

报错

解决方案

publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE)

 问题

解决

publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE, default=1)

 

posted @ 2019-07-23 20:58  市丸银  阅读(638)  评论(0编辑  收藏  举报