Accessing a django model in middleware (django>1.7) -


i'm upgrading django 1.6.5 django 1.9, , in process upgrading several middleware classes. of middleware classes use models during process_request or process_response phases. however, i'm getting appregistrynotready: apps aren't loaded yet. error attempting use them.

is there way import models during middleware?
should move import statements process_request / process_response methods?

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>     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. 

you need use new api wsgi handler:

from django.core.wsgi import get_wsgi_application  application = get_wsgi_application() 

this call django.setup() you, populate app registry.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -