Schemamigration:添加一个简单的Null字段

时间:2014-06-05 18:17:15

标签: python django django-models django-south

好的,所以我有一个名为locationmanager的应用程序,其模型如下所示:

class Location(models.Model):
    region = models.ForeignKey(Region)

我想把它改成这个

class Location(models.Model):
    region = models.ForeignKey(Region, blank=True, null=True)

所以我运行了一个schemamigration并收到了以下内容:

$ python manage.py schemamigration locationmanager --auto
 ? The field 'Location.region' does not have a default specified, yet is NOT NULL.
 ? Since you are making this field nullable, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now.
 ?  2. Specify a one-off value to use for existing columns now
 ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
 ? Please select a choice: 

这让我感到困惑。是的,我已阅读南方文档。

  1. 首先,为什么要说'Location.region' does not have a default specified, yet is NOT NULL.我对该字段做出的改动是否使它成为空的,是什么给出的?解释会很棒。

  2. 为什么我需要指定默认值?一旦我指定了这个默认值它在哪里?在SQL表中?如果我有现有数据或仅仅是为了方案本身,这是否重要?

  3. 3.我有点理解选项1,2但是选项3究竟做了什么?为什么有用?

    非常感谢。

1 个答案:

答案 0 :(得分:1)

因为迁移双向工作。有一天你可能想要回滚到之前的状态,在这种情况下你需要一个必填字段的默认值。

哦,在这种情况下,我通常总是选择 2 选项并输入0作为值。工作正常。