django admin自定义命令 - 将字符串列表传递给args

sna*_*ies 3 python django

我有一个字符串列表,我想在我的django自定义命令中传递给args.

list = ['abc', 'def', 'ghi', etc...]
Run Code Online (Sandbox Code Playgroud)

我怎样才能在python函数中执行此操作:

management.call_command('commandname', args, options) 
Run Code Online (Sandbox Code Playgroud)

我已经厌倦了传递我的args列表:

[1]直接:

    management.call_command('commandname', list)
Run Code Online (Sandbox Code Playgroud)

[2]作为循环:

    management.call_command('commandname', (abc for abc in list))
Run Code Online (Sandbox Code Playgroud)

但是在输入自定义命令后两者都失败了

mat*_*ath 9

call_command方法使用Arbitrary Arguments List作为命令参数.

所以,你需要使用:

list = ['abc', 'def', 'ghi']
management.call_command('commandname', *list)
Run Code Online (Sandbox Code Playgroud)

哪个是相同的:

management.call_command('commandname', 'abc', 'def', 'ghi')
Run Code Online (Sandbox Code Playgroud)

相关信息: