I have been reading how to write a django management command by reading this.
I did also read the django documentation but I am wondering if it's possible to write few commands to run using only one file?
instead of writing a few different files.
I have been reading how to write a django management command by reading this.
I did also read the django documentation but I am wondering if it's possible to write few commands to run using only one file?
instead of writing a few different files.
I don't think so. If I'm reading the source correctly commands are discovered from CLI by looking for modules:
https://github.com/django/django/blob/master/django/core/management/init.py#L26
Which collects using:
https://docs.python.org/2/library/pkgutil.html#pkgutil.iter_modules
You can write multiple commands in one file. Follow below steps
from django.core.management.base import BaseCommand
from subprocess import Popen
from sys import stdout, stdin, stderr
import time
import os
import signal
class Command(BaseCommand):
help = 'Run all commands'
commands = [
'python manage.py schedule',
'python manage.py runserver'
]
def handle(self, *args, **options):
proc_list = []
for command in self.commands:
print("$ " + command)
proc = Popen(command, shell=True, stdin=stdin,
stdout=stdout, stderr=stderr)
proc_list.append(proc)
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
for proc in proc_list:
os.kill(proc.pid, signal.SIGKILL)
python manage.py mycommandThis link helped me.