python - "No installed app with label ' 管理员 '"运行 Django 迁移。该应用程序已正确安装

标签 python django django-admin django-migrations

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

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

在外壳上,它可以工作:

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

但是在迁移过程中,它失败了:

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

这样失败:

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

使用调试器,我发现“管理员”没有安装:

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 doc说清楚:

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),
    ]

关于python - "No installed app with label ' 管理员 '"运行 Django 迁移。该应用程序已正确安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29565665/

相关文章:

python - boto3 s3 客户端记录的 close() 方法丢失?

python - 管理 Mac OS 在 Windows 环境中使用非 ASCII 字符创建的文件名?

python - Django 中的 SortedDict

python - 如果对象存在,我如何获取它,或者如果它在 Django 中不存在,我如何获取它?

django - 如何允许通过自定义 list_display 字段在 Django admin 中进行排序,该字段没有 DB 字段也没有可注释

python - 如何使用python docx在ms word中设置表格的单元格边距

python - Django REST Framework TokenAuthentication 通常在 Django 中进行身份验证

python - Django 获取 IP 只返回 127.0.0.1

Django Admin - 与主模型一起验证内联

python - 使用 OneToOneField 扩展 Django Admin UserCreationForm