我使用自定义逻辑进行的迁移尝试使用Model3
中的otherapp
,但失败并显示LookupError: App 'otherapp' doesn't have a 'model3' model.
Model3不是新模型,它已经使用了一段时间,我可以在迁移过程中访问otherapp
中的其他模型。
from __future__ import unicode_literals
from django.db import migrations
def update_items(apps, schema_editor):
# apps shows references to models in 'otherapp'
# This prints Model1, Model2 but is missing Model3
print apps.all_models['otherapp'].keys()
# This works
OtherAppModel1 = apps.get_model('otherapp', 'Model1')
# This fails: LookupError: App 'otherapp' doesn't have a 'model3' model.
OtherAppModel3 = apps.get_model('otherapp', 'Model3')
# This works but should not be done in a migration
from otherapp.models import Model3
class Migration(migrations.Migration):
dependencies = [
('thisapp', '0102_foo'),
]
operations = [
migrations.RunPython(update_items)
]