12
I need to create a custom command that I can run with manage.py. How do I create one?
Loading...
Q&A · Answered
I need to create a custom command that I can run with manage.py. How do I create one?
Accepted answer
myapp/
management/
__init__.py
commands/
__init__.py
mycommand.py
# 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!'))
python manage.py mycommand --name JohnSay what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.