问题描述
我有一个模型
类神秘(models.Model):
first = models.CharField(max_length = 256)
second = models.CharField(max_length = 256)
third = models.CharField(max_length = 256)
player = models.ForeignKey(Player)
我添加了播放器ForeignKey,但是当我尝试使用南方迁移它,似乎我无法创建一个null = False 。我有这个消息:
我使用这个命令:
非常感谢!
另一个选择是创建一个在添加ForeignKey之前,您可以在其中创建具有特定ID的新的Player实例。确保该数据库以前不存在于您的数据库中。
1.创建数据迁移文件
$ ./manage.py datamigration myapp add_player
创建的00XX_add_player.py
2.编辑文件的向前和向后方法:
code> def forward(self,orm):
orm ['myapp.Player']。objects.create(name = u'Very misterious player',id = 34)
def backwards(self,orm):
#还没有测试过这个
orm ['myapp.Player']。objects.filter(id = 34).delete()
$ / pre>
3.将ForeignKey添加到您的Mistery类并再次迁移该架构。它将要求您的数据迁移ID的默认值,在此示例中为34。
$ ./manage.py schemamigration --auto myapp
?字段'Mistery.player'没有指定默认值,但是不为空。
?因为你添加这个字段,你必须指定一个默认的
?用于现有行的值。你想要:
? 1.现在退出,并在models.py
中的字段中添加默认值? 2.现在指定一个一次性值用于现有的列
?请选择一个选项:2
?请输入您的一次性默认值的Python代码。
? datetime模块是可用的,所以你可以做例如datetime.date.today()
>>> 34
+在myapp.Mistery上添加了字段播放器
创建了0010_auto__add_field_mistery_player.py。您现在可以使用以下方式应用此迁移:./manage.py migrate myapp
4.最后运行迁移命令,它将按顺序执行迁移,插入新播放器并更新所有Mistery行,并引用新播放器。
I have a Model
class Mystery(models.Model): first = models.CharField(max_length=256) second = models.CharField(max_length=256) third = models.CharField(max_length=256) player = models.ForeignKey(Player)
I added the player ForeignKey but when I try to migrate it using South it seems that I can't create this whith a null=False. I have this message :
I use this command :
Thanks a lot !
解决方案Another option is to create a data migration before adding the ForeignKey, in which you create a new Player instance with a specific id. Be sure that that id does not exist previously in your database.
1.Create the data migration file
$ ./manage.py datamigration myapp add_player Created 00XX_add_player.py
2.Edit the forwards and backwards methods of the file:
def forwards(self, orm): orm['myapp.Player'].objects.create(name=u'Very misterious player', id=34) def backwards(self, orm): # Haven't tested this one yet orm['myapp.Player'].objects.filter(id=34).delete()
3.Add the ForeignKey to your Mistery class and migrate the schema again. It will ask for the default value to your data migration id, in this example, 34.
$ ./manage.py schemamigration --auto myapp ? The field 'Mistery.player' does not have a default specified, yet is NOT NULL. ? Since you are adding this field, you MUST specify a default ? value to use for existing rows. Would you like to: ? 1. Quit now, and add a default to the field in models.py ? 2. Specify a one-off value to use for existing columns now ? Please select a choice: 2 ? Please enter Python code for your one-off default value. ? The datetime module is available, so you can do e.g. datetime.date.today() >>> 34 + Added field player on myapp.Mistery Created 0010_auto__add_field_mistery_player.py. You can now apply this migration with: ./manage.py migrate myapp
4.Finally run the migrate command and it will execute the migrations in sequential order, inserting the new Player and updating all the Mistery rows with the reference to your new Player.
这篇关于Django南 - 创建不是空的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!