Custom Management Command
A snippet showing how to build custom Django management commands.
# myapp/management/commands/hello.py
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Prints a greeting message'
def add_arguments(self, parser):
parser.add_argument('name', type=str, help='Name to greet')
def handle(self, *args, **options):
name = options['name']
self.stdout.write(self.style.SUCCESS(f'Hello, {name}!'))
Explanation:
-
Create
app/management/commands/directory with__init__.pyfiles. -
Subclass
BaseCommandand implementhandle()method. -
Run with
python manage.py hello <name>.
- Category Utilities & Miscellaneous
- Total Views 104
- Last Modified 06 June, 2026
- Tags #management #commands #utilities #scripts
Previous snippet