Django 从 1.8 升级到 1.9 在 init 中破坏模型导入
Django upgrade from 1.8 to 1.9 is breaking on model import in init
我 运行 遇到了与@JohnnyQ 评论 相同的问题。
我的 __init__.py
指的是多个导入,而这些导入又指的是模型,这些是必需的。
该代码在 django 1.8.x 上运行良好,但在 Django 升级到 1.9.x 时会中断。
我猜这是一些 class 加载顺序问题。 1.9 w.r.t 中对触发 AppRegistryNotReady 异常的应用程序中的导入进行建模有何变化?
回溯在这里:
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 引入了 app registry,如文档中所述,它分 3 个步骤进行初始化。
首先,它导入 INSTALLED_APPS 中的所有项目。在此阶段,代码不应导入任何模型。
其次,Django 尝试导入每个应用程序的模型子模块(如果存在)。
- 第三,Django 在每个应用程序配置上运行
.ready()
。
在 core/__init__.py
中,有 care_team
导入,依次导入此 from django.contrib.auth.models import User
。应在应用程序级别初始化时避免导入此模型。
这应该延迟加载。或者需要重构代码,使得在应用程序初始化期间不会导入任何模型。
我 运行 遇到了与@JohnnyQ 评论
我的 __init__.py
指的是多个导入,而这些导入又指的是模型,这些是必需的。
该代码在 django 1.8.x 上运行良好,但在 Django 升级到 1.9.x 时会中断。
我猜这是一些 class 加载顺序问题。 1.9 w.r.t 中对触发 AppRegistryNotReady 异常的应用程序中的导入进行建模有何变化?
回溯在这里:
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 引入了 app registry,如文档中所述,它分 3 个步骤进行初始化。
首先,它导入 INSTALLED_APPS 中的所有项目。在此阶段,代码不应导入任何模型。
其次,Django 尝试导入每个应用程序的模型子模块(如果存在)。
- 第三,Django 在每个应用程序配置上运行
.ready()
。
在 core/__init__.py
中,有 care_team
导入,依次导入此 from django.contrib.auth.models import User
。应在应用程序级别初始化时避免导入此模型。
这应该延迟加载。或者需要重构代码,使得在应用程序初始化期间不会导入任何模型。