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

snakesNbronies
  • 3,619
  • 9
  • 44
  • 73

1 Answers1

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:

Community
  • 1
  • 1
math
  • 2,811
  • 3
  • 24
  • 29
  • Can you give a example using what I've specified above? Sorry this just isn't very helpful without an example. – snakesNbronies May 22 '12 at 07:19
  • Another example: http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/#calling-a-function – arie May 22 '12 at 07:21
  • Just call it like this: `management.call_command('commandname', 'abc', 'def', 'ghi')` – Reinout van Rees May 22 '12 at 07:25
  • 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. – snakesNbronies May 22 '12 at 07:36
  • @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. – XORcist May 22 '12 at 08:15