Django 1.8 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

來源:互聯網
上載者:User

標籤:

在進行 Django 日常開發時,經常會建立一個獨立的 PY 檔案用來測試寫好的方法.

# mysite/main.py# -*- coding:utf-8 -*-from deploy.getInfo import *from getRunTime import *def main():    # 擷取域    print getDomain()    # 擷取網域名稱對應規則    print getDomainRewrite("1")if __name__ == ‘__main__‘:    main()

啟動 Django 時報錯

Traceback (most recent call last):  File "E:\MyEclipse_work\ops\deploy\main.py", line 35, in <module>    main()  File "E:\MyEclipse_work\ops\deploy\main.py", line 12, in main    print getDomain()  File "E:\MyEclipse_work\ops\deploy\deploy\getInfo.py", line 10, in getDomain    return json.dumps(list(deploy.objects.filter().values("domain_id__domain_name","domain_id")))  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\query.py", line 640, in values    return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\query.py", line 992, in _clone    c._setup_query()  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\query.py", line 1169, in _setup_query    self.query.add_fields(self.field_names, True)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\sql\query.py", line 1660, in add_fields    name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\sql\query.py", line 1443, in setup_joins    names, opts, allow_many, fail_on_missing=True)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\sql\query.py", line 1347, in names_to_path    field, model, _, _ = opts.get_field_by_name(name)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py", line 406, in get_field_by_name    cache = self.init_name_map()  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py", line 435, in init_name_map    for f, model in self.get_all_related_m2m_objects_with_model():  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py", line 520, in get_all_related_m2m_objects_with_model    cache = self._fill_related_many_to_many_cache()  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py", line 534, in _fill_related_many_to_many_cache    for klass in self.apps.get_models():  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\utils\lru_cache.py", line 101, in wrapper    result = user_function(*args, **kwds)  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py", line 168, in get_models    self.check_models_ready()  File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py", line 131, in check_models_ready    raise AppRegistryNotReady("Models aren‘t loaded yet.")django.core.exceptions.AppRegistryNotReady: Models aren‘t loaded yet.

最後的報錯為: django.core.exceptions.AppRegistryNotReady: Models aren‘t loaded yet.

報錯資訊來自於 registry.py 的斷言.

File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py", line 131, in check_models_ready  raise AppRegistryNotReady("Models aren‘t loaded yet.")

因此查看 C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py 檔案中查看 raise AppRegistryNotReady

def check_models_ready(self):    """    Raises an exception if all models haven‘t been imported yet.    """    if not self.models_ready:        raise AppRegistryNotReady("Models aren‘t loaded yet.")

通過注釋可以看出引起異常的原因是因為 models 沒有被 import

於是在程式開頭 import 處加入了

from deploy.models import *

再次運行報錯依舊.

在官網文檔中搜尋 AppRegistryNotReady 異常時發現關於 Standalone scripts 的描述.

簡單來講,Django 測試依賴於 DJANGO_SETTINGS_MODULE 環境變數. 如果我們想要使用單獨的 PY 指令碼來測試 Django,那麼應該在我們的程式開始初始化 Django 環境.否則就會報出 AppRegistryNotReady 異常.

# 初始化命令import djangodjango.setup()

增加之後運行正常.

隨即查看了 django.setup() 源碼.

from django.utils.version import get_versionVERSION = (1, 8, 0, ‘alpha‘, 0)__version__ = get_version(VERSION)def setup():    """    Configure the settings (this happens as a side effect of accessing the    first setting), configure logging and populate the app registry.    """    from django.apps import apps    from django.conf import settings    from django.utils.log import configure_logging    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)    apps.populate(settings.INSTALLED_APPS)

關於 django.setup() 方法的官方描述

 

 

參考文檔

  ■  https://docs.djangoproject.com/en/1.8/releases/1.7/#app-loading-changes

  ■  https://docs.djangoproject.com/en/1.8/ref/applications/#django.setup

Django 1.8 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.