首先,迁移按创建迁移文件时定义的顺序进行。
# 0001_initial.py
...
operations = [
migrations.CreateModel(
name=modelA,
....
),
migrations.CreateModel(
name=modelB,
....
),
]
您可以检查迁移文件并确保
modelA
是以前
modelB
.
其次,
modelA.get_active_attribute()
需要一个数据库条目才能返回某些内容。运行迁移时,不插入数据。所以你不应该宣布
default
其他模型的对象。
你应该重写
save()
确保默认值基于
模特儿
的属性。
class modelB(models.Model):
attribute = models.IntegerField()
def save(self, *args, **kwargs):
if self.attribute is None:
self.attribute = modelA.get_active_attribute()
super(modelB, self).save(*args, **kwargs)