我正在尝试一个简单的模型继承:
class Product(models.Model):
creation_date = models.DateTimeField(auto_now_add=True)
class Application(Product):
name = models.CharField(max_length=200)
'makemigrations'要求提供默认值:
您正尝试将非可空字段'product_ptr'添加到 没有默认的申请;我们做不到(数据库需要 填充现有行的东西。)
我看到here我可以使用Meta类生成一个抽象模型,但我不能这样做,因为我特别在其他模型中作为实际模型引用它:
class Comment(models.Model):
product = models.ForeignKey('Product', related_name="comments")
删除数据库时运行'makemigrations'也会导致同样的问题。
我能做什么?
Django 1.9
答案 0 :(得分:2)
您还没有解释您所做的更改究竟是什么,似乎您已将Application
模型更改为继承自之前从models.Model继承的Product
。这导致django在幕后创建1对1映射。您没有添加到模型中的product_ptr会自动进入图片
参考:https://docs.djangoproject.com/en/1.9/topics/db/models/#multi-table-inheritance
继承关系引入了子模型之间的链接 及其每个父母(通过自动创建的OneToOneField)。
在迁移过程中将此字段添加到包含数据的表中有点棘手,因为此字段必须是唯一的。如果您只是创建一个名为Application
的新模型,则值为1即可。