Django "AppRegistryNotReady: Apps aren't loaded yet" in CI
Django raises AppRegistryNotReady when code that needs the app registry (importing a model, querying, calling get_user_model) runs before django.setup() finishes loading apps. Order of import, not configuration, is the problem.
What this error means
A script, conftest, or Celery module fails at import time with "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet."
python
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.Common causes
ORM access at module import time
A module calls get_user_model() or runs a query at top level, which executes during import before apps finish loading.
A standalone entry point without setup
A CI script imports Django models without first calling django.setup(), so the registry is empty.
How to fix it
Call django.setup() before touching the ORM
- Set
DJANGO_SETTINGS_MODULE, then calldjango.setup()at the very start of standalone scripts. - Move
get_user_model()and queries out of module top level into functions called at runtime. - Defer model imports until inside functions where needed.
python
import django
django.setup()
def run():
from django.contrib.auth import get_user_model
User = get_user_model() # called after setup, not at importLet the runner initialize Django
Use manage.py test or pytest-django, which call setup before any test module imports run.
How to prevent it
- Never run queries or call get_user_model at module top level.
- Always django.setup() first in standalone scripts.
- Import models lazily inside functions where ordering is fragile.
Related guides
Django "RuntimeError: Model class doesn't declare an explicit app_label" in CIFix Django "RuntimeError: Model class ... doesn't declare an explicit app_label and isn't in an application i…
Django "ImproperlyConfigured: SECRET_KEY must not be empty" in CIFix Django "django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty" in CI - se…
Celery "Received unregistered task of type" in CIFix Celery "Received unregistered task of type 'X'. The message has been ignored and discarded" in CI - the w…