How to create custom Django management commands

Answered
Jan 05, 2026 824 views 1 answers
12

I need to create a custom command that I can run with manage.py. How do I create one?

P
Asked by python_dev
Platinum 447 rep

1 Answer

10

Create command file structure

myapp/
    management/
        __init__.py
        commands/
            __init__.py
            mycommand.py

Write the command

# myapp/management/commands/mycommand.py
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'Description of my command'

    def add_arguments(self, parser):
        parser.add_argument('--name', type=str)

    def handle(self, *args, **options):
        name = options.get('name', 'World')
        self.stdout.write(f'Hello, {name}!')
        self.stdout.write(self.style.SUCCESS('Done!'))

Run the command

python manage.py mycommand --name John
H
Answered by hassan_ops 1 week, 2 days ago
Bronze 565 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer

Related Questions

Hot Questions

No hot questions available.