A management command is a Python script run through `manage.py` to control a Django project without a web interface. Built-in commands support tasks such as project setup, database work, testing, debugging, fixture handling, and deployment; custom commands are useful for batch jobs, cleanup, and offline work that needs the project’s configured models. section 9.0 section 9.1
Django discovers a custom command inside an installed app’s `management/commands` package. The command’s filename becomes its command name. Its module contains a class derived from `BaseCommand`; Django calls `handle()` when `python manage.py command_name` runs. Set `help` for generated command help, and write output through `self.stdout.write()` or `self.stderr.write()` rather than `print()` so output is easier to test. section 9.2 section 9.6
Command-line inputs fall into two useful categories:
- Positional arguments provide values according to their place in the command line, commonly data or filtering criteria. section 9.3
- Flags are optional behavior switches, usually beginning with `-` or `--`; Django supplies options such as `--help`, while `add_arguments()` adds command-specific options. section 9.3 section 9.6
List musicians from project data
Place `musicians.py` under the bands app’s `management/commands` directory. A `Command(BaseCommand)` can query `Musician.objects.all()` in `handle()` and print each musician’s name. Add `--last_name`, `--first_name`, and `--birth` flags to filter results; invalid birth-date input can raise `CommandError`. The command runs as `python manage.py musicians`. section 9.2 section 9.3
Table 9.1 Common management commands that are built into Django
|
Command
|
App
|
Description
|
|---|---|---|
changepassword
|
auth
|
Change a user’s password. Doesn’t require authentication.
|
check
|
django
|
Runs a check on the project. This is the same check the development server runs on startup.
|
clearsessions
|
sessions
|
Removes expired sessions from the database
|
collectstatic
|
staticfiles
|
Copies static files to the configured
|
compilemessages
|
django
|
Used to create translation files
|
createcachetable
|
django
|
Creates a database table to be used for page caching
|
createsuperuser
|
auth
|
Creates a superuser (admin) account in the database
|
dbshell
|
django
|
Opens your database’s command-line tool
|
diffsettings
|
django
|
Displays differences between your settings and the defaults
|
dumpdata
|
django
|
Exports data from the database into a fixture
|
findstatic
|
staticfiles
|
Searches for a named static file and prints its details
|
flush
|
django
|
Removes all data from the database except migration state
|
inspectdb
|
django
|
Outputs a models.py file based on an existing table
|
loaddata
|
django
|
Imports a fixture into the database
|
makemessages
|
django
|
Builds translation files based on strings in the source
|
makemigrations
|
django
|
Creates migration files based on changes to Models
|
migrate
|
django
|
Performs migrations on the database
|
optimizemigration
|
django
|
Optimizes a migration file
|
remove_stale_ contenttypes
|
contenttypes
|
Removes unused content type entries if the
contenttypes app is in use
|
runserver
|
django
|
Runs the development server
|
sendtestemail
|
django
|
Sends a test email
|
shell
|
django
|
Runs a Django-aware REPL
|
showmigrations
|
django
|
Shows migrations in a project
|
sqlflush
|
django
|
Prints the SQL commands that are run when the
flush management command executes
|
sqlmigrate
|
django
|
Prints the SQL for a named migration
|
sqlsequencereset
|
django
|
Prints the SQL for resetting an index sequence
|
squashmigrations
|
django
|
Squishes multiple migrations into a single file
|
startapp
|
django
|
Creates a new Django app directory
|
startproject
|
django
|
Creates a new Django project
|
test
|
django
|
Runs unit tests
|
testserver
|
django
|
Runs a development server but using a test database populated with the named fixture’s deployment destination
|
Browse common built-in commands for administration, checks, sessions, static files, data export, and database utilities. section 9.1.1
