如何将默认值设置为自动生成的OneToOneField关系?

时间:2019-08-05 08:28:00

标签: python django django-models

我有两个他们喜欢的模型类:


from django.db import models
from django.urls import reverse

class Place(models.Model):
    address = models.CharField(max_length=80)


class Author(Place):
    name = models.CharField(max_length=50)

但是当我想执行makemigartions时,django向我显示以下错误:

You are trying to add a non-nullable field 'place_ptr' to author without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option: 

我在Author表中有一些数据,但是我不想删除这些数据。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您可以删除特定应用的旧迁移文件或整个迁移文件夹,然后执行python manage.py makemigrations <app_name>
如果这不是您想要的解决方案,那么you can find the solution here.