加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django 1.9:在`django.setup()`中我应该避免导入模型

发布时间:2020-12-20 12:32:56 所属栏目:Python 来源:网络整理
导读:将我的应用程序移植到 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
将我的应用程序移植到 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/__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.

我的主要问题在这里:

我应该在我的django应用程序的__init__.py中导入我的模型吗?

它似乎触发了django.models.ModelBase元类,它在创建模型之前检查应用程序是否准备就绪.

解决方法

Should I import my models in the __init__.py of my django apps ?

不,您不得在任何已安装的应用程序的__init__.py文件中导入任何模型.这在1.9中已不再可能.

从release notes:

All models need to be defined inside an installed application or
declare an explicit app_label. Furthermore,it isn’t possible to
import them before their application is loaded. In particular,it isn’t possible to import models inside the root package of an application.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读