user2194805
user2194805

Reputation: 1369

Django call command with parameters

I've a management command with the following argument parser section:

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('--out', action='store', dest='out', required=True)
        parser.add_argument('--todos', action='store', dest='todos', \
                required=True, nargs='+')
        parser.add_argument('--filter', action='store', dest='filter')
        parser.add_argument('--list', action='store', dest='list')
        parser.add_argument('--add_id', action='store_true', dest='add_id')

. I'm trying to call this command from a view / shell, but does not work:

from django.core.management import call_command
call_command('prg_name', out='/tmp/111.txt')

, however i got the following error: argument --out is required .

The program is kinda old: python: 2.7 Django: 1.11.16

.

How should i call the call_command with parameters with this old layout?

What is strange, if i do this:

dt = {'--out=/tmp/111.txt': sth}
call_command(prg_name, *dt)

it works, however i can't define more value for --todos .

Upvotes: 5

Views: 2271

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476534

You can just pass an arbitrary number of strings to the command:

call_command(prg_name, '--out=/tmp/111.txt', '--todos', '8', '12', '105', '51')

You can see these as the parameters that you would have passed to your program if you called it from the command line.

Upvotes: 7

Related Questions