2016-11-17 103 views
0

我從django 1.6.5升級到django 1.9,並且在升級過程中幾個中間件類。其中一些中間件類在process_requestprocess_response階段使用模型。但是,我收到一個AppRegistryNotReady: Apps aren't loaded yet.錯誤嘗試使用它們。在中間件中訪問django模型(django> 1.7)

有沒有辦法在中間件導入模型?
我應該將我的進口報表轉換成process_request/process_response方法嗎?

Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_ 
    result = wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_ 
    result = wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 158, in __call__ 
    self.load_middleware() 
    File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/common/object_wrapper.py", line 302, in _wrapper 
    result = wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 51, in load_middleware 
    mw_class = import_string(middleware_path) 
    File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 20, in import_string 
    module = import_module(module_path) 
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/web/MyJobs/MyJobs/apache/../middleware.py", line 9, in <module> 
    from django.contrib.sites.models import Site 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 83, in <module> 
    class Site(models.Model): 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 94, in __new__ 
    app_config = apps.get_containing_app_config(module) 
    File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 239, in get_containing_app_config 
    self.check_apps_ready() 
    File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 124, in check_apps_ready 
    raise AppRegistryNotReady("Apps aren't loaded yet.") 
AppRegistryNotReady: Apps aren't loaded yet. 
+0

這可能是您的'wsgi.py'問題。你使用'get_wsgi_application()'還是舊的'WSGIHandler()'? – knbk

+0

@knbk謝謝。我正在使用舊的處理程序。作出回答,我會接受它。 – mklauber

回答

1

您需要使用新的API來獲取一個WSGI處理程序:

from django.core.wsgi import get_wsgi_application 

application = get_wsgi_application() 

這將調用django.setup()對你來說,這將填充應用註冊表。