Reputation: 1369
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
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