Django Routines¶
Configure batches of Django management commands in your settings files and run them all at once. For example, batch together your common database maintenance tasks, deployment routines or any other set of commands you need to run together. This helps single source general site maintenance into your settings files keeping your code base DRY.
If you have ever written one management command that calls other management commands this package may help you.
Example
Let’s define two named routines, “package” and “deploy”. The package routine will be a collection of commands that we typically run to generate package artifacts (like migrations and transpiled javascript). The deploy routine will be a collection of commands we typically run when deploying the site for the first time on a new server or when we deploy version updates on the server.
Note
Routine commands are run in the order they are registered, or by priority.
There are two types of commands, management commands and system commands. The management commands
will be called in the same process space as routine unless --subprocess is specified in which
case they will use the same management script as routine was invoked with or whatever value you
supply to --manage-script. System commands are always invoked as subprocesses.
In our settings file we may define these routines like this:
1from django_routines import (
2 ManagementCommand,
3 SystemCommand,
4 command,
5 system,
6 routine
7)
8
9# register routines and their help text
10routine(
11 name="package",
12 help_text=(
13 "Generate pre-package artifacts like migrations and transpiled "
14 "javascript."
15 )
16)
17# you may register commands on a routine after defining a routine (or before!)
18command("package", "makemigrations")
19command("package", "renderstatic")
20system("package", "poetry", "build")
21
22routine(
23 "deploy",
24 "Deploy the site application into production.",
25
26 # you may also specify commands inline using the ManagementCommand dataclass
27 ManagementCommand(
28 ("routine", "package"), switches=["prepare"]
29 ), # routine commands can be other routines!
30 ManagementCommand("migrate"),
31 ManagementCommand("collectstatic"),
32 ManagementCommand(("shellcompletion", "install"), switches=["initial"]),
33 ManagementCommand(("loaddata", "./fixtures/demo.json"), switches=["demo"]),
34 SystemCommand(("touch", "/path/to/wsgi.py")),
35
36 # define switches that toggle commands on and off
37 prepare="Generate artifacts like migrations and transpiled javascript.",
38 initial="Things to do on the very first deployment on a new server.",
39 demo="Load the demo data.",
40)
The routine command will read our settings file and generate two subcommands, one called deploy and one called package:
Now we can run all of our package routines with one command:
?> django-admin routine package
makemigrations
...
renderstatic
...
poetry build
...
The deploy command has several switches that we can enable to run additional commands.
For example to deploy our demo on a new server we would run:
?> django-admin routine deploy --initial --demo
migrate
...
collectstatic
...
shellcompletion install
...
loaddata ./fixtures/demo.json
...
touch /path/to/wsgi.py
Settings
The ManagementCommand dataclass, routine() and
command() helper functions in the example above make it easier for us to
work with the native configuration format which is a dictionary structure defined in the
DJANGO-ROUTINES setting attribute. For example the above configuration is equivalent to:
1
2DJANGO_ROUTINES = {
3 "deploy": {
4 "commands": [
5 {"management": ("routine", "package"), "switches": ["prepare"]},
6 {"management": "migrate"},
7 {"management": "collectstatic"},
8 {
9 "management": ("shellcompletion", "install"),
10 "switches": ["initial"],
11 },
12 {
13 "management": ("loaddata", "./fixtures/demo.json"),
14 "switches": ["demo"],
15 },
16 {"system": ("touch", "/path/to/wsgi.py")},
17 ],
18 "help_text": "Deploy the site application into production.",
19 "switch_helps": {
20 "demo": "Load the demo data.",
21 "initial": "Things to do on the very first deployment on a new "
22 "server.",
23 "prepare": "Generate artifacts like migrations and transpiled "
24 "javascript.",
25 },
26 },
27 "package": {
28 "commands": [
29 {"management": "makemigrations"},
30 {"management": "renderstatic"},
31 {"system": ("uv", "build")},
32 ],
33 "help_text": "Generate pre-package artifacts like migrations and "
34 "transpiled javascript.",
35 },
36}
Priorities
If you are composing settings from multiple apps or source files using a utility like django-split-settings you may not be able to define all routines at once. You can use priorities to make sure commands defined in a de-coupled way run in the correct order.
command("deploy", "makemigrations", priority=1)
command("deploy", "migrate", priority=2)
Options
When specifying arguments you may add them to the command tuple OR specify them as named
options in the style that will be passed to call_command():
# these two are equivalent
command("package", ("makemigrations", "--no-header"))
command("package", "makemigrations", no_header=True)
Note
Lazy translations work as help_text for routines and switches.
Execution Controls
There are several switches that can be used to control the execution of routines. Pass these parameters when you define the Routine.
atomic: Run the routine in a transaction.continue_on_error: Continue running the routine even if a command fails.
The default routine behavior for these execution controls can be overridden on the command line.
Pre/Post Hooks
PreHook and PostHook functions can be attached to
routines and commands. These functions provide:
a way to execute arbitrary code before or after the routine or command execution.
full access to the routine or command context (options and results).
the ability to halt execution of the routine early or skip individual commands.
the ability to modify the routine or command context.
Rationale
When does it make sense to configure routines in Django settings? Its generally convenient to group common management pathways into easily discoverable and executable aggregations of subroutines. This is usually done in supporting shell scripts or just files and in most cases that is appropriate. If your goal is to keep your Django deployment as tight and self contained as possible and the deployment is not generally very complex, using django-routines can make a lot of sense. It can eliminate extra dependencies on a shell scripting environment or just files and can keep this logic packaged with your installable wheel.
Django routines also encourages stronger command logic encapsulation. It allows complex workflows to be broken down into smaller, more manageable commands that can be attached and invoked together as part of larger routines.
Contents:
- Installation
- Settings
- Signals
- Reference
- Change Log
- v1.7.1 (2026-03-05)
- v1.7.0 (2025-11-22)
- v1.6.2 (2025-09-27)
- v1.6.1 (2025-07-25)
- v1.6.0 (2025-07-22)
- v1.5.1 (2025-07-17)
- v1.5.0 (2025-05-28)
- v1.4.0 (2024-04-02)
- v1.3.0 (2024-02-18)
- v1.2.1 (2024-08-26)
- v1.2.0 (2024-07-27)
- v1.1.3 (2024-07-17)
- v1.1.2 (2024-07-15)
- v1.1.1 (2024-07-15)
- v1.1.0 (2024-07-10)
- v1.0.2 (2024-06-05)
- v1.0.1 (2024-06-05)
- v1.0.0 (2024-06-05)