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)