Django的call_command失败,缺少必需的参数 [英] Django's call_command fails with missing required arguments
问题描述
django.core.management.call_command
。它不起作用。 我有一个带有4个必需参数的命令。当我打电话时,它抱怨所有的争论都丢失,即使我传递他们:
call_command('my_command',url = '12',project ='abc',website ='zbb',title ='12345')
我得到基本的命令错误--url,--project,--website和--title丢失。
我没有指定不同的目的地这些论据。
我查看了 call_command
源码,并将问题精确定位到 call_command中的以下行
:
如果command.use_argparse:
#使用`dest`选项名称解析器选项
opt_mapping = {sorted(s_opt.option_strings)[0] .lstrip(' - ')。replace(' - ','_'):s_opt.dest
for s_opt in parser。 _actions如果s_opt.option_strings}
arg_options = {opt_mapping.get(key,key):key的值,在options.items()中的值)
defaults = parser.parse_args(args = args)** ****这个*****
defaults = dict(defaults._get_kwargs(),** arg_options)
#将位置参数从选项中移出以模仿旧版optparse
args = defaults .pop('args',())
args
是传递给call_commands的位置参数,它是空的。我只传递命名参数。
这是在Django 1.8.3中。
这是我的命令的add_arguments函数(为了简洁起见,我刚刚删除了帮助字符串):
def add_arguments(self,parser):
parser.add_argument(' - url',action ='store',required = True)
parser.add_argument(' - project',action = 'store',required = True)
parser.add_argument(' - continue-processing',action ='store_true',default = False)
parser.add_argument(' - website',action = 'store',required = True)
parser.add_argument(' - title',action ='store',required = True)
parser.add_argument(' - duplicate',action ='store_true ',default = False)
你发布的代码,我已经在 call_command参数是必需的
所需的命名参数必须通过 * args
,而不仅仅是位置。
** kwargs
绕过解析器。所以没有看到你在那里定义的任何东西。 ** kwargs
可以覆盖 * args
值,但 * args
仍然需要每个必需参数的东西。如果您不想这样做,请关闭所需的
属性。
I want to call a Django management command from one of my tests. I'm using django.core.management.call_command
for this. And it doesn't work.
I have a command with 4 required arguments. When I call it, it complains all arguments are missing even though I'm passing them:
call_command('my_command', url='12', project='abc', website='zbb', title='12345')
I get the base command error that --url, --project, --website and --title are missing.
I did not specify a different destination for these arguments.
I looked at the call_command
source and pinpointed the problem to the following line in call_command
:
if command.use_argparse:
# Use the `dest` option name from the parser option
opt_mapping = {sorted(s_opt.option_strings)[0].lstrip('-').replace('-', '_'): s_opt.dest
for s_opt in parser._actions if s_opt.option_strings}
arg_options = {opt_mapping.get(key, key): value for key, value in options.items()}
defaults = parser.parse_args(args=args) ****** THIS *****
defaults = dict(defaults._get_kwargs(), **arg_options)
# Move positional args out of options to mimic legacy optparse
args = defaults.pop('args', ())
args
is the positional arguments passed to call_commands, which is empty. I'm only passing named arguments. parser.parse_args
complains the required variables are missing.
This is in Django 1.8.3.
Here is my command's add_arguments function (I just removed the help strings for brevity):
def add_arguments(self, parser):
parser.add_argument('--url', action='store', required=True)
parser.add_argument('--project', action='store', required=True)
parser.add_argument('--continue-processing', action='store_true', default=False)
parser.add_argument('--website', action='store', required=True)
parser.add_argument('--title', action='store', required=True)
parser.add_argument('--duplicate', action='store_true',default=False)
Based on that piece of code which you posted, I've concluded in call_command argument is required
that the required named arguments have to be passed in through *args
, not just the positionals.
**kwargs
bypasses the parser. So it doesn't see anything you defined there. **kwargs
may override the *args
values, but *args
still needs something for each required argument. If you don't want to do that, then turn off the required
attribute.
这篇关于Django的call_command失败,缺少必需的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!