Python Django 1.9:我应该避免在'Django.setup()期间导入模型吗?
将我的应用程序移植到django 1.9,我得到了可怕的Python Django 1.9:我应该避免在'Django.setup()期间导入模型吗?,python,django,django-models,django-1.9,Python,Django,Django Models,Django 1.9,将我的应用程序移植到django 1.9,我得到了可怕的django.core.exceptions.AppRegistryNotReady:应用程序尚未加载 基本上,我的跟踪是: manage.py execute_from_command_line(sys.argv) django/core/management:352, in execute_from_command_line utility.execute() django/core/management/_
django.core.exceptions.AppRegistryNotReady:应用程序尚未加载
基本上,我的跟踪是:
manage.py
execute_from_command_line(sys.argv)
django/core/management:352, in execute_from_command_line
utility.execute()
django/core/management/__init__.py:326, in execute
django.setup()
django/__init__.py:18, in setup
apps.populate(settings.INSTALLED_APPS)
django/apps/registry.py:85, in populate
app_config = AppConfig.create(entry)
django/apps/config.py:90, in create
module = import_module(entry)
python2.7/importlib/__init__.py:37, in import_module
__import__(name)
myapp/mylib/__init__.py:52, in <module>
from django.contrib.contenttypes.models import ContentType #<= The important part
django/contrib/contenttypes/models.py:159, in <module>
class ContentType(models.Model):
django/db/models/base.py:94, in __new__
app_config = apps.get_containing_app_config(module)
django/apps/registry.p:239, in get_containing_app_config
self.check_apps_ready()
django/apps/registry.py:124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
manage.py
从命令行(sys.argv)执行命令
django/core/management:352,从命令行执行
utility.execute()
django/core/management/_init__.py:326,在execute中
django.setup()
django/_uuuuinit_uuuuuuu.py:18,在设置中
应用程序。填充(设置。已安装的应用程序)
django/apps/registry.py:85,在填充中
app_config=AppConfig.create(条目)
django/apps/config.py:90,在create中
模块=导入模块(条目)
python2.7/importlib/_init__u;.py:37,在导入模块中
__导入(名称)
myapp/mylib/__init__.py:52,在
从django.contrib.contenttypes.models导入ContentType#
我应该在django应用程序的\uuuu init\uuuuu.py
中导入我的模型吗
否,必须不要导入任何已安装应用程序的\uuuuu init\uuuuuuuuy.py
文件中的任何模型。这在1.9中不再可能
从:
所有模型都需要在已安装的应用程序或应用程序中定义
声明一个明确的应用程序标签。此外,这是不可能的
在加载应用程序之前导入它们特别是
无法在的根包中导入模型
应用程序。