使用迁移,我需要向模型添加新字段(外键).我知道可以用:
migrations.AddField(
model_name='MyModel',
name='state',
field=models.ForeignKey(null=True, related_name='mymodel_state', to='msqa_common.MyModelState'),
),
Run Code Online (Sandbox Code Playgroud)
但是,我不希望我的字段可以为空.相反,我想使用它的默认值,对应于名称为"available"的MyModelState的id(id值可能在不同的机器中更改).表MyModelState的此"可用"值将在先前的迁移脚本中插入到数据库中,因此它确实存在.
我想我应该这样做:
migrations.AddField(
model_name='MyModel',
name='state',
field=models.ForeignKey(null=False, default=available_state_id, related_name='mymodel_state', to='msqa_common.MyModelState'),
),
Run Code Online (Sandbox Code Playgroud)
我的问题:我怎样才能获得available_state_id我的迁移脚本?