Search code examples
python-3.xdjangodjango-manage.pydjango-commandsdjango-4.1

How to combine multiple custom management commands in Django?


I wrote a set of commands for my Django project in the usual way. Is it possible to combine multiple commands into one command?

class Command(BaseCommand):
    """ import all files in media in dir and add to db
    after call  process to generate thumbnails  """

    def handle(self, *args, **options):
       ...

To make easy steps I have commands like: import files, read metadata from files, create thumbnails etc. The goal now is to create a "do all" command that somehow imports these commands and executes them one after another.

How to do that?


Solution

  • You can define a DoAll command and use django django.core.management.call_command to run all your subcommands. Try something like below:

    class FoobarCommand(BaseCommand):
        """ call foo and bar commands """
    
        def handle(self, *args, **options):
            call_command("foo_command", *args, **options)
            call_command("bar_command", *args, **options)