“未安装带有标签“ admin”的应用”运行Django迁移。该应用已正确安装 [英] "No installed app with label 'admin'" running Django migration. The app is installed correctly

查看:124
本文介绍了“未安装带有标签“ admin”的应用”运行Django迁移。该应用已正确安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django 1.7上进行数据迁移时使用admin.LogEntry对象

I am trying to use admin.LogEntry objects during a datamigration on Django 1.7

'django.contrib.admin'应用列在 INSTALLED_APPS 上。

在外壳上,它的工作原理是:

On the shell, it works:

>>> from django.apps import apps
>>> apps.get_model('admin', 'LogEntry')
django.contrib.admin.models.LogEntry

但是在迁移期间,它会失败:

But during the migration, it fails:

def do_it(apps, schema_editor):
    LogEntry = apps.get_model('admin', 'LogEntry')

失败,像这样:

django-admin migrate
(...)
LookupError: No installed app with label 'admin'.

使用调试器,我没有安装'admin':

Using a debugger, I got that the 'admin' is not installed:

ipdb> apps.get_apps()
[]
ipdb> apps.all_models.keys()
['website', 'google', 'allauth', 'twitter', 'busca', 'conteudo', 'django_mobile', 'django_filters', 'videocenter', 'tinymce', 'oferta', 'programacaotv', 'contenttypes', 'suit', 'haystack', 'destaque', 'filer', 'galeria', 'auth', 'facebook', 'paintstore', 'critica', 'disqus', 'fichas', 'omeletop', 'autocomplete_light', 'modelsv1', 'temas', 'django_extensions', 'adv_cache_tag', 'taggit', 'social', 'personalidade']

为什么?

推荐答案

Django文档使其成功清除:


编写 RunPython 函数时,该函数使用的应用程序不同于移植应用程序中的应用程序迁移的依赖项属性(位于其中)应包含所涉及的每个应用程序的最新迁移,否则可能会出错或类似于: LookupError:尝试使用 apps.get_model()函数在 RunPython 函数中检索模型时,未安装标签为 myappname的应用程序

When writing a RunPython function that uses models from apps other than the one in which the migration is located, the migration’s dependencies attribute should include the latest migration of each app that is involved, otherwise you may get an error similar to: LookupError: No installed app with label 'myappname' when you try to retrieve the model in the RunPython function using apps.get_model().

代码示例:

# Imports are omitted for the sake of brevity

def move_m1(apps, schema_editor):
    LogEntry = apps.get('admin.logentry')
    # Other business logic here ...


class Migration(migrations.Migration):

    dependencies = [
        ('app1', '0001_initial'),

        # Below is the manually added dependency, so as to retrieve models
        # of 'django.contrib.admin' app with apps.get_model() in move_m1().
        #
        # Currently this is for Django 1.11. You need to look into
        # 'django/contrib/admin/migrations' directory to find out which is
        # the latest migration for other version of Django.
        ('admin', '0002_logentry_remove_auto_add'),
    ]

    operations = [
        migrations.RunPython(move_m1),
    ]

这篇关于“未安装带有标签“ admin”的应用”运行Django迁移。该应用已正确安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆