Django 的 call_command 因缺少必需参数而失败 [英] Django's call_command fails with missing required arguments

查看:25
本文介绍了Django 的 call_command 因缺少必需参数而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的一个测试中调用 Django 管理命令.为此,我正在使用 django.core.management.call_command.它不起作用.

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.

我有一个带有 4 个必需参数的命令.当我调用它时,它抱怨所有参数都丢失了,即使我正在传递它们:

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')

我收到基本命令错误,即 --url、--project、--website 和 --title 丢失.

I get the base command error that --url, --project, --website and --title are missing.

我没有为这些参数指定不同的目的地.

I did not specify a different destination for these arguments.

我查看了 call_command 源代码并将问题定位到 call_command 中的以下行:

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 是传递给 call_commands 的位置参数,为空.我只传递命名参数.parser.parse_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.

这是在 Django 1.8.3 中.

This is in Django 1.8.3.

这是我的命令的 add_arguments 函数(为简洁起见,我刚刚删除了帮助字符串):

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)

推荐答案

根据您发布的那段代码,我在 call_command 参数是必需的

Based on that piece of code which you posted, I've concluded in call_command argument is required

必须通过 *args 传入所需的命名参数,而不仅仅是位置.

that the required named arguments have to be passed in through *args, not just the positionals.

**kwargs 绕过解析器.所以它看不到你在那里定义的任何东西.**kwargs 可能会覆盖 *args 值,但是 *args 仍然需要为每个必需的参数提供一些东西.如果您不想这样做,请关闭 required 属性.

**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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆