简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM