django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Exception appeared when I added:
import signals
in init.py file (apps/application/init.py)from models import Review
in signals.py file (apps/application/signals.py)
I want to send an http request when there is an insert in the model Review.
So I need to import Review model (in the __init.py__
file) to execute the following code:
@receiver (pre_save, sender = Review)
def my_handler (sender, ** kwargs):
....
In the Django's source, this is where the exception is comming from:
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:
Hence, what you can do is override one of the
AppConfig
method calledAppConfig.ready()
which allow you to perform initialization tasks such as registering 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 toTrue
only after the registry is fully populated and allAppConfig.ready()
methods are called.