带有附加参数的自定义 django 管理命令 [英] Custom django management command with appended parameters

查看:28
本文介绍了带有附加参数的自定义 django 管理命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队经常运行自定义测试套件.当我们想运行整个事情时,我们这样做

My team runs a custom test suite pretty regularly. When we want to run the whole thing, we do

./manage.py test --keepdb --failfast --settings=AE.test_settings

当我们想在特定应用上运行测试时,我们也会这样做,但要包含应用名称.

When we want to run tests on a specific app, we do the same, but with the app name include.

我想创建一个自定义管理命令,在调用时将运行默认测试套件,但附加 --keepdb、--failfast 和 --settings=AE.test_settings 参数.理想情况下像

I'd like to make a custom management command that, when called, will run the default test suite, but append the --keepdb, --failfast, and --settings=AE.test_settings params. Ideally something like

./manage.py aetest

也可以使用应用名称运行,例如

That can also be run with app names, such that

./manage.py aetest registration

和跑步一样

./manage.py test registration --keepdb --failfast --settings=AE.test_settings

到目前为止我的尝试是

from django.core.management.base import BaseCommand, CommandError
from django.core import management

class Command(BaseCommand):

    def handle(self, *args, **kwargs):
        positional_arguments = [
            '--keepdb',
            '--failfast',
            '--settings=NJ.test_settings',
        ]
        for item in args:
            positional_arguments.append(item)
        keyword_arguments = {
            'settings': "AE.test_setings",  
        }
        keyword_arguments.update(kwargs)
        management.call_command("test", *positional_arguments, **keyword_arguments)

如果我尝试传递更多的位置参数,我的命令会出错,暗示它们从未被传递过.并且关键字参数永远不会进入 call_command(该命令使用我的默认 settings.py 文件运行)

If I try to pass further positional arguments, my command errors out in such a way as to imply they were never passed. And the keyword argument never makes it to the call_command (the command runs with my default settings.py file)

知道如何让这个管理命令接受额外的位置和关键字参数,但自动附加所需的参数吗?

Any idea how I can have this management command accept additional positional and keyword parameters, but append the desired ones automatically?

谢谢!

更新

我得到了将 BaseTestCommand 子类化的好主意.虽然这似乎适用于 True/False 变量,但传递设置变量似乎不起作用.如果我使用以下代码:

I was given the great idea of subclassing BaseTestCommand. While this seems to work for True/False variables, passing the settings variable doesn't seem to work. If I use the following code:

class Command(BaseTestCommand):
    def handle(self, *test_labels, **options):
        options['failfast'] = True
        options['keepdb'] = True
        options['settings'] = "AE.test_settings"
        print(options)

        super(Command, self).handle(*test_labels, **options)

运行我的新命令使用 failfast 和 keepdb,但它仍然使用我的项目的默认 settings.py.知道为什么会这样吗?

Running my new command uses the failfast and the keepdb, but it still uses the default settings.py for my project. Any idea why this might be?

旁注:使用 --settings=AE.test_settings 运行新命令就像一个魅力

Side note: running the new command with --settings=AE.test_settings works like a charm

推荐答案

你可以子类化 Django test 命令并覆盖 handle 方法以设置您需要的选项.

You could subclass the Django test command and override the handle method to set the options you need.

from django.core.management.commands.test import Command as BaseTestCommand


class Command(BaseTestCommand):
    def handle(self, *test_labels, **options):
        options['failfast'] = True
        options['keepdb'] = True
        options['settings'] = 'NJ.test_settings'

        super(Command, self).handle(*test_labels, **options)

至于为什么你的代码不起作用,那是因为你滥用了call_command函数.每个命令选项(以 -- 开头)都必须作为关键字参数传递,即使它是布尔值,你的 handle 方法应该是:

As to why your code does not work, it is because your are misusing the call_command function. Every command option (that starts with --) must be passed as a keyworded argument even if it is boolean, your handle method should then be:

def handle(self, *args, **kwargs):
    kwargs.update({
        'settings': 'NJ.test_settings',
        'keepdb': True,
        'failfast': True
    })
    management.call_command("test", *args, **kwargs)

这篇关于带有附加参数的自定义 django 管理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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