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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…