'makemigrations' 要求使用 none 抽象模型继承的默认值

'makemigrations' asks for a default with none-abstract model inheritance

我正在尝试一个简单的模型继承:

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 生成抽象模型,但我不能这样做,因为我在其他模型中专门将其作为实际模型引用:

class Comment(models.Model):
    product      = models.ForeignKey('Product', related_name="comments")

运行 'makemigrations' 删除数据库也会导致同样的问题。

有什么我可以做的吗?

Django 1.9

您没有解释您所做的更改到底是什么,看来您已经将 Application 模型更改为从 Product 继承,而它之前是从 models.Model 继承的].这导致 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).

在迁移期间将此字段添加到包含数据的 table 中有点棘手,因为此字段需要是唯一的。如果您只是创建一个名为 Application 的新模型,那么值 1 就可以了。