pythonpython-2.7django-1.9django-contribdjango-upgrade

Django upgrade from 1.8 to 1.9 is breaking on model import in init


I have run into the same problem that @JohnnyQ has commented on here.
My __init__.py is referring to multiple imports that in-turn are referring to models and these are required.

The code works perfectly fine with django 1.8.x but breaks when Django is upgraded to 1.9.x.

I am guessing it to be some class-loading sequence issue. What has changed in 1.9 w.r.t to model imports in apps triggering the AppRegistryNotReady exception?

The traceback is here:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 729, in __call__
    ret = self.original_func(*self.args, **self.kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/__init__.py", line 1, in <module>
    from mycompany.core import care_team
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/care_team.py", line 8, in <module>
    from mycompany.core import models as core_models
  File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/models.py", line 16, in <module>
    from django.contrib.auth.models import User
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Solution

  • Django 1.9 introduced app registry, which is initialized in 3 steps as mentioned in the documentation.

    In core/__init__.py, there is care_team import which is, in turn, importing this from django.contrib.auth.models import User. This model import should be avoided at app level initialization.

    This should be loaded lazily. Or code needs to be refactored such that no model will be imported during app initialization.