ala*_*jds 25 python django django-admin django-migrations
我试图在Django 1.7上的数据迁移期间使用admin.LogEntry对象
该'django.contrib.admin'
应用程序已列出INSTALLED_APPS
.
在shell上,它的工作原理是:
>>> from django.apps import apps
>>> apps.get_model('admin', 'LogEntry')
django.contrib.admin.models.LogEntry
Run Code Online (Sandbox Code Playgroud)
但在迁移过程中,它失败了:
def do_it(apps, schema_editor):
LogEntry = apps.get_model('admin', 'LogEntry')
Run Code Online (Sandbox Code Playgroud)
失败如下:
django-admin migrate
(...)
LookupError: No installed app with label 'admin'.
Run Code Online (Sandbox Code Playgroud)
使用调试器,我得到了'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']
Run Code Online (Sandbox Code Playgroud)
为什么??
小智 27
只需setting.py
在已安装的应用程序部分检查您的应用程序,并确保您已在其中添加您的应用程序:
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'--- you need to add your app here ---',
]
Run Code Online (Sandbox Code Playgroud)
Roc*_*ite 19
在Django的文档清楚:
在编写RunPython函数时,该函数使用的模型不是迁移所在的应用程序,迁移的依赖项属性应包括所涉及的每个应用程序的最新迁移,否则您可能会收到类似于以下内容的错误:LookupError:No installed当您尝试使用apps.get_model()在RunPython函数中检索模型时,带有标签'myappname'的应用程序.
代码示例:
# 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),
]
Run Code Online (Sandbox Code Playgroud)
我不知道确切的原因.将不得不深入挖掘源代码.但是现在一个解决方法是添加
('admin', 'name_of_last_migration_in_admin_app')
到依赖项,并且迁移应该没有问题.