3

I have a list of strings which I would like to pass into args in my django custom command.

list = ['abc', 'def', 'ghi', etc...]

How can I do this from within a python function:

management.call_command('commandname', args, options) 

I've tired passing on my list of args both:

[1] directly:

    management.call_command('commandname', list)

and

[2] as a loop:

    management.call_command('commandname', (abc for abc in list))

but both have failed after entering the custom command

1 Answer 1

10

The call_command method is using Arbitrary Arguments List for command arguments.

So, you need to use:

list = ['abc', 'def', 'ghi']
management.call_command('commandname', *list)

Which is the same than:

management.call_command('commandname', 'abc', 'def', 'ghi')

Related information:

Sign up to request clarification or add additional context in comments.

5 Comments

Can you give a example using what I've specified above? Sorry this just isn't very helpful without an example.
Just call it like this: management.call_command('commandname', 'abc', 'def', 'ghi')
Is there a max number of args for a django command? I have a list of 500 args and it's crashing at the end. A test with 10 args worked perfectly.
@thong i just tried to pass 10^7 *args and 10^7 *kwargs to a normal python function and it still worked. not sure about django functions peculiarities though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.