Django 6.1.3

13.1 Commands, checks, and testing

manage.py, discovery, and BaseCommand

What runs between typing python manage.py ... and entering BaseCommand.handle, and why do some commands skip django.setup or system checks?

Every "manage.py" in every Django repository is practically identical: it sets "DJANGO_SETTINGS_MODULE" via "os.environ.setdefault" and hands "sys.argv" straight to execute_from_command_line, which constructs ManagementUtility and calls ManagementUtility.execute.

Bootstrapping settings, get_commands, and app override precedence

Trace the exact order inside ManagementUtility.execute:

  1. Early option pre-parsing: Before importing settings or booting apps, Django creates a minimal CommandParser to extract "--settings" and "--pythonpath" so "handle_default_options" can mutate "os.environ" and "sys.path" early enough to affect which apps and commands exist.
  2. Conditional django.setup(): If "settings.configured" is True (having just loaded "INSTALLED_APPS"), Django calls django.setup() — with one deliberate exception on lines 393–414: when running "runserver" without "--noreload", ManagementUtility.execute wraps django.setup in django.utils.autoreload.check_errors and swallows startup exceptions by populating empty "apps.app_configs" so the parent autoreloader can still start, watch your broken file, and restart cleanly once you save the fix! Commands run without valid settings (like "django-admin startproject" or "--version") skip django.setup() completely.
  3. Command discovery (get_commands, get_commands): Scans "django/core/management/commands/" first, then iterates reversed(apps.get_app_configs()) scanning each app’s "management/commands/" directory. Walking "INSTALLED_APPS" in reverse means the earliest app in "INSTALLED_APPS" wins any name collision — which is exactly why "django.contrib.staticfiles" overrides "django.core"'s built-in "runserver" command to serve static files automatically in development!
  4. Command loading (ManagementUtility.fetch_command, ManagementUtility.fetch_command): Imports "<app>.management.commands.<name>" via load_command_class and instantiates its "Command" subclass of BaseCommand.

BaseCommand.run_from_argv, execute, and check gating

Next ManagementUtility calls BaseCommand.run_from_argv, which parses the full argparse CLI flags, calls BaseCommand.execute, intercepts CommandError to print a clean error message and sys.exit(e.returncode) (unless "--traceback" was passed), and always executes "connections.close_all()" inside its "finally:" block so short-lived CLI scripts never leak open database sockets!

Inside BaseCommand.execute, two class attributes decide what guards run before your BaseCommand.handle:

Watch get_commands and ManagementUtility.fetch_command inspect "desk/"'s 31 discovered commands and their exact check flags live:

Recorded on Django 6.1.3 ch13-tooling script
1. ManagementUtility command discovery and BaseCommand flags
  discovered_commands=31 runserver_app='django.contrib.staticfiles' migrate_app='django.core'
  requires_system_checks: check=[] migrate='__all__' runserver='__all__' diffsettings=[]
  requires_migrations_checks: migrate=False runserver=False check=False

Notice "runserver_app='django.contrib.staticfiles'" confirming reverse-"INSTALLED_APPS" command precedence, and "check=[]" avoiding recursive double-checking inside "manage.py check".

Next we open that exact check engine — seeing how CheckRegistry separates everyday static checks from "--deploy" security guards and "database" checks — in The system check framework and --deploy guards.