Django "AppRegistryNotReady: Apps aren't loaded yet" in CI
Django populates its app registry during django.setup(). If a standalone CI script imports models or calls ORM APIs before that, the registry is empty and Django raises AppRegistryNotReady.
What this error means
A migration data step, seed script, or custom runner fails with "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet." even though manage.py commands work.
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.Common causes
Models imported before django.setup()
A script imports from app.models import Thing at module top level without configuring Django first, so the app registry has not been built.
ORM access at import time in a helper
A module runs a query or apps.get_model() while being imported, before apps finish loading.
How to fix it
Call django.setup() before importing models
- Set DJANGO_SETTINGS_MODULE at the top of the script.
- Call
django.setup(). - Only then import models or touch the ORM.
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.ci")
django.setup()
from app.models import Thing # safe nowRun the task through manage.py
A custom management command runs after Django is fully set up, so the registry is already populated.
python manage.py seed_demo_dataHow to prevent it
- Never import models at the top of a standalone script before django.setup().
- Move one-off tasks into management commands.
- Defer ORM access until inside a function, not module import.