Django 1.7 upgrade error: AppRegistryNotReady: Models aren't loaded yet

回眸只為那壹抹淺笑 提交于 2019-11-27 13:14:01

The problem is with this line ("/Users/Name/Dev/tps/products/models.py", line 127):

watson.register(Product.objects.exclude(productimage=None))

You try to reference a model at the import time. It is no longer possible in Django 1.7. Django 1.7 allows you to use your models only after all of the applications are loaded. You should move this call to the ready callback of AppConfig, like this:

from django.apps import AppConfig


class ProductsConfig(AppConfig):
    name = 'products'

    def ready(self):
        Product = self.get_model('Product')
        watson.register(Product.objects.exclude(productimage=None))

Then you should reference this AppConfig in the __init__.py of your products app:

default_app_config = 'products.apps.ProductsConfig'

Where apps is the name of the module where you put the config.

Relevant Django doc: https://docs.djangoproject.com/en/dev/ref/applications/

Overall, because of this change, migrating to Django 1.7 is not as easy as one would like it to be. Here are some troubleshooting tips: https://docs.djangoproject.com/en/1.7/ref/applications/#troubleshooting

I was getting this error when I updated my django project template to 1.7. One thing that changed is the wsgi.py file so that needed some updating. Here is my traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 93, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 134, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/wsgi.py", line 168, in __call__
    self.load_middleware()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/handlers/base.py", line 46, in load_middleware
    mw_instance = mw_class()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/middleware/locale.py", line 23, in __init__
    for url_pattern in get_resolver(None).url_patterns:
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/kpmteam/staging/site/kpm/urls.py", line 7, in <module>
    admin.autodiscover()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/utils/module_loading.py", line 67, in autodiscover_modules
    for app_config in apps.get_app_configs():
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/home/kpmteam/staging/deploys/20141029-115625/site/lib/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.

This is what the wsgi.py file looked like:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

And now (running Django 1.7 with gunicorn 19.1.0):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Changing the last two lines fixed it. Note your DJANGO_SETTINGS_MODULE might be different, typically it is "project_name.settings"

wouldnt

I'm late to the party, but if you're using django-registration, you'll have to switch to django-regitration-redux.

https://pypi.python.org/pypi/django-registration-redux/

See this answer: Django-registration compatibility issue with django 1.7

I found the solution doing:

import django

django.setup()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!