无法从另一个django app持续时间迁移中访问模型

时间:2016-10-04 15:24:53

标签: python django migration

我使用自定义逻辑进行的迁移尝试使用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)
    ]

0 个答案:

没有答案