Django从1.8升级到1.9的init模型导入失败 [英] Django upgrade from 1.8 to 1.9 is breaking on model import in init

查看:145
本文介绍了Django从1.8升级到1.9的init模型导入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了@JohnnyQ在此处评论的相同问题.
我的__init__.py指的是多个导入,而这些导入又是指模型,而这些是必需的.

I have run into the same problem that @JohnnyQ has commented on here.
My __init__.py is referring to multiple imports that in-turn are referring to models and these are required.

该代码在django 1.8.x上运行良好,但是在Django升级到1.9.x时中断.

The code works perfectly fine with django 1.8.x but breaks when Django is upgraded to 1.9.x.

我猜这是一些类加载序列的问题.在1.9 w.r.t中发生了什么变化,以对触发AppRegistryNotReady异常的应用程序中的导入进行建模?

I am guessing it to be some class-loading sequence issue. What has changed in 1.9 w.r.t to model imports in apps triggering the AppRegistryNotReady exception?

回溯在这里:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 729, in __call__
    ret = self.original_func(*self.args, **self.kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/__init__.py", line 1, in <module>
    from mycompany.core import care_team
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/care_team.py", line 8, in <module>
    from mycompany.core import models as core_models
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/models.py", line 16, in <module>
    from django.contrib.auth.models import User
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

推荐答案

Django 1.9引入了

Django 1.9 introduced app registry, which is initialized in 3 steps as mentioned in the documentation.

  • 首先,它导入INSTALLED_APPS中的所有项目.在此阶段,代码不应导入任何模型.

  • First, it imports all items in INSTALLED_APPS. At this stage, code shouldn’t import any models.

第二,Django尝试导入每个应用程序的模型子模块(如果存在).

Second, Django attempts to import the models submodule of each application, if they are present.

core/__init__.py中,有care_team导入,然后又导入了该from django.contrib.auth.models import User.在应用程序级别初始化时,应避免导入此模型.

In core/__init__.py, there is care_team import which is, in turn, importing this from django.contrib.auth.models import User. This model import should be avoided at app level initialization.

这应该延迟加载.或者需要重构代码,以便在应用初始化期间不会导入任何模型.

This should be loaded lazily. Or code needs to be refactored such that no model will be imported during app initialization.

这篇关于Django从1.8升级到1.9的init模型导入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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