Django与南迁:意想不到的字段出来了

Django and south migration: unexpected field came out

我使用 Django 1.6 作为后端,并使用 South 进行迁移。 我有两个 table,ContentChannel,我在 Channel 中添加了一个字段 adress:

class Channel(models.Model):
  name = models.CharField(max_length=256)
  adress = models.CharField(max_length=256,null=True)

我在table中添加了一个ForeignKey Content:

class Content(models.Model):
  name = models.CharField(max_length=256)
  channel = models.ForeignKey(Channel, null=True, blank=True)

当我运行命令时:

python manage.py schemamigration myapp --auto

我知道了:

 + Added field adress on myapp.Channel
 ? The field 'Content.adress' does not have a default specified, yet is NOT NULL.
 ? Since you are removing this field, 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: Connection to v-atm-t3v closed by remote host.

为什么我有字段 Content.adress?我将 ForeignKey 标记为 null=True,blank=True 因为我希望它默认为空白,我该如何解决?谢谢!

该消息基本上是说:您之前在模型 Content 中有一个名为 adress 的字段,并且当您定义它时它不能为空(您没有 null=True 在该字段上)。当你删除它的时候,south需要知道你在恢复这个字段的时候要填什么值,以防你要逆向迁移。

如果您了解 south,它有 forwardbackward 方法,分别应用和还原更改。当您想要撤消迁移时,无法恢复模型 Content 上字段 adress 的数据。但是你已经让它不可为空,south 需要填写一些值,因此提示消息。

解决方案取决于您的需要。如果您不费心恢复该字段的值,选择 3 就可以了,因为您永远不会 运行 向后迁移。或者选择 2 但它并不比 3 更好,只需为所有记录填写一个默认值,这很可能不是您想要的。