Search code examples
pythondjangodjango-commands

Can I combine custom django-admin commands?


I have several custom command classes that subclass BaseCommand. I use those in a specific order to populate the DB.

I want to write another command to combine those in a single one. I don't want to write a shell script that will execute the commands in the right order. I want it to be a django-admin command.

I know I can just throw the code of all commands in a single one but I am looking for a more generic way to do it. So that I will have all the population scripts per db table clean and separated.


Solution

  • You could write another command, and use call_command for each command you want to run.

    from django.core import management
    

    Then in your management command:

    management.call_command('my_first_command')
    management.call_command('my_second_command')
    ...
    

    If your management commands take args or kwargs you'll need to do a bit more work.