在迁移回滚期间无法从其他应用程序导入 Django 模型

SS_*_*ous 2 python django python-3.x django-migrations

我正在创建一个数据迁移,并且new_app可以将其回滚。

# This is `new_app` migration
class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.RunPython(import_data, reverse_code=delete_data)
    ]
Run Code Online (Sandbox Code Playgroud)

此迁移将一些数据添加到其他应用程序中定义的模型中:my_other_app。要导入我想要更新或删除记录的模型,我使用apps.get_model()方法。

# This is `new_app` migration
def import_data(apps, schema_editor):   
    model = apps.get_model('my_other_app', 'MyModel')
Run Code Online (Sandbox Code Playgroud)

当我应用迁移时,它就像魅力一样。但是当我运行尝试回滚迁移时,出现:~> manage.py migrate new_app zero异常:LookupError: No installed app with label 'my_other_app'.回滚代码中的模型导入:

# This is `new_app` migration
def delete_data(apps, schema_editor):
    schema_model = apps.get_model('my_other_app', 'MyModel')
Run Code Online (Sandbox Code Playgroud)

模型导入的代码是一样的,但是为什么迁移回滚时不起作用?现在我import在回滚期间有一个使用直接模型的解决方法。不知道会不会给以后带来麻烦。

mes*_*shy 5

确保其中dependencies包含来自您引用的其他应用程序的最新迁移。例如:

dependencies = [
    'my_other_app.0001_initial',
]
Run Code Online (Sandbox Code Playgroud)

另外,请确保'my_other_app'在您的INSTALLED_APPS设置中。