django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. file init

不打扰是莪最后的温柔 提交于 2019-11-29 11:50:39

In the Django's source, this is where the exception is comming from:

def check_apps_ready(self):
    """Raise an exception if all apps haven't been imported yet."""
    if not self.apps_ready:
        raise AppRegistryNotReady("Apps aren't loaded yet.")

As you can see it make sure every apps are ready (loaded). In general, when it is related to signals there are normally 2 situations when this happen.

  • Circular imports

    Make sure there are none in your project. This can cause the error.

  • Registering signal before the app is loaded

    See this for more information. But, one thing that help me understanding how Django works behind the scene is this statement:

It is important to understand that a Django application is just a set of code that interacts with various parts of the framework. There’s no such thing as an Application object. However, there’s a few places where Django needs to interact with installed applications, mainly for configuration and also for introspection. That’s why the application registry maintains metadata in an AppConfig instance for each installed application.

Hence, what you can do is override one of the AppConfig method called AppConfig.ready() which allow you to perform initialization tasks such as registering signals.

# yourApp/__init__.py

default_app_config = 'yourappname.apps.YourAppConfig'

# yourApp/apps.py
from django.apps import AppConfig

class YourAppConfig(AppConfig):
    name = 'yourappname'

    def ready(self):
        from yourappname import signals

Further information

For the sake of explanation, this is the recommend way of doing since Django 1.7+ which is most likely your case. The logic behind it is that the application registry hold a boolean value ready which is set to True only after the registry is fully populated and all AppConfig.ready() methods are called.

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