关于django.setup()的调用位置、作用及解决apps.get_models()触发AppRegistryNotReady报错的咨询
Hey there! Let's tackle your Django questions step by step—they all revolve around Django's app registry initialization, which is a common point of confusion when working outside the standard request/response flow.
django.setup()? You only need to call django.setup() when you're running standalone Python scripts that interact with Django components (like models, ORM operations, or app registry tools) but aren't launched via Django's built-in commands (e.g., manage.py runserver, manage.py shell).
Django automatically handles initialization when you use its official commands, but standalone scripts don't trigger this process. So you need to manually kick off Django's setup to load settings, initialize the app registry, and make models available.
Here's a typical example of a standalone script using django.setup():
import os import django from django.apps import apps # Point Django to your project's settings module os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings') # Initialize Django's core components django.setup() # Now you can safely interact with models or use app registry tools from your_app.models import YourModel all_models = apps.get_models()
Important: Never call django.setup() inside settings.py—this will cause circular dependencies or premature initialization, since settings.py is part of the setup process itself.
django.setup()'s role, django_cleanup integration, and fixing the AppRegistryNotReady error What does django.setup() do?
This function bootstraps Django's core system:
- It loads your project settings
- Initializes the app registry (which tracks all installed apps and their models)
- Triggers app-specific initialization hooks (like the
ready()method inapps.py) - Makes Django's ORM and other components available for use
Do I need to call it for django_cleanup?
Nope! django_cleanup works by hooking into Django's model signals, and as long as you've added 'django_cleanup.apps.CleanupConfig' to your INSTALLED_APPS in settings.py, Django will automatically load and initialize it when the app registry is ready. No need to call django.setup() in settings.py for this (or any other app, for that matter).
Fixing django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
This error happens when you try to access models or the app registry before Django has finished initializing. The fix depends on where you're running the code:
Case 1: You're in a standalone script
Follow the example from question 1: set the DJANGO_SETTINGS_MODULE environment variable, call django.setup(), then run apps.get_models() after setup completes. This ensures the app registry is fully loaded.
Case 2: You're within your Django project (e.g., views, signals, or module-level code)
If you're calling apps.get_models() at the module level (outside a function), Django might execute that code before all models are loaded. Instead:
- Move the
apps.get_models()call inside a function that runs later (like a view function, or a signal handler) - Or use your app's
ready()method (inapps.py), which Django calls after all models are loaded:
# your_app/apps.py from django.apps import AppConfig from django.apps import apps class YourAppConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'your_app' def ready(self): # This runs after all models are registered all_models = apps.get_models() # Do your processing here
内容的提问来源于stack exchange,提问作者Ivan

