Skip to content
Latchkey

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

  1. Set DJANGO_SETTINGS_MODULE, then call django.setup() at the very start of standalone scripts.
  2. Move get_user_model() and queries out of module top level into functions called at runtime.
  3. 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 import

Let 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →