我正在尝试一个简单的模型继承:
class Product(models.Model):
creation_date = models.DateTimeField(auto_now_add=True)
class Application(Product):
name = models.CharField(max_length=200)
'makemigrations' 请求默认值:
You are trying to add a non-nullable field 'product_ptr' to application without a default; we can't do that (the database needs something to populate existing rows).
我看到了here我可以使用 Meta 类生成抽象模型,但我不能这样做,因为我在其他模型中专门将其作为实际模型引用:
class Comment(models.Model):
product = models.ForeignKey('Product', related_name="comments")
在删除数据库时运行“makemigrations”也会导致同样的问题。
有什么我可以做的吗?
Django 1.9
请您参考如下方法:
您还没有解释您所做的更改到底是什么,似乎您已经将 Application 模型更改为继承自 Product ,它之前继承自模型。模型。这导致 django 在幕后创建一个 1 对 1 的映射。自己没有添加到模型中的product_ptr的添加进入图片
引用:https://docs.djangoproject.com/en/1.9/topics/db/models/#multi-table-inheritance
The inheritance relationship introduces links between the child model and each of its parents (via an automatically-created OneToOneField).
在迁移过程中将此字段添加到包含数据的表中有点棘手,因为此字段需要是唯一的。如果您只是创建一个名为 Application 的新模型,则值为 1 就可以了。
