使用 args 和选项编写自定义管理命令 - 所需字段说明 [英] Writing a custom management command with args and options - explanation of fields needed

查看:14
本文介绍了使用 args 和选项编写自定义管理命令 - 所需字段说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 django 应用程序中,我正在编写一个自定义管理命令,它将根据传递的 args 创建一个对象的实例,并可以根据选项 --save 是否通过.

In my django app I am writing a custom management command which will create an instance of an object based on the args passed and have the option of saving it to the database based on whether an option --save is passed or not.

在这方面我得到了很多帮助django 文档 本身.从这里获得了关于如何传递多个参数和这里了解如何选择.

I've got a lot of help on this from the django documentation itself. Have also got important pointers from here about how to pass multiple arguments and here about how to have options.

from optparse import make_option

class Command(BaseCommand):
  option_list = BaseCommand.option_list + (
    make_option('--delete',
        action='store_true',
        dest='delete',
        default=False,
        help='Delete poll instead of closing it'),
    )

  def handle(self, *args, **options):
    # ...
    if options['delete']:
        poll.delete()
    # ...

但是,我无法在 make_option 中找到有关字段的详细说明.例如 optparse.make_option 列表

However I am not able to find a detailed explanation of the fields in make_option. For example optparse.make_option lists

Instance attributes:
_short_opts : [string]
_long_opts : [string]

action : string
type : string
dest : string
default : any
nargs : int
const : any
choices : [string]
callback : function
callback_args : (any*)
callback_kwargs : { string : any }
help : string
metavar : string

在这个 help 中是不言自明的,我知道 dest 是什么意思,但我不清楚 action='store_true' 的意思.事实上,如果有人能给我一个简短的描述 make_option 的所有参数的含义,那就太好了......

In this help is self explanatory and I figured out what dest would mean, but I'm not clear what action='store_true' means. In fact it'd be great if someone could give me a short description of what all the arguments to make_option mean...

非常感谢

推荐答案

optparse docs 可能会更有帮助.您基本上是在告诉管理功能您需要的每个选项应该做什么.

The optparse docs might be a little bit more helpful. You are basically telling the management function what each option you require should do.

动作 关键字是最有说服力的,它配置了你想用那个选项做什么——它只是一个做一些特殊事情的标志(一个callback,即'--enable-feature')还是它是否应该接受一个参数,例如(store,即'-things 10').

The action keyword is the most telling and it configures what you want to do with that option - is it just a flag to do something special (a callback, i.e. '--enable-feature') or should it accept a parameter for example (store, i.e. '-things 10').

考虑到这一点,考虑到这一点,其余选项会更有意义.通读 'option attributes' 以获得对您的解释'已经列出,然后'actions' 看看我上面提到的

With that in mind, the rest of the options make a bit more sense with that all in mind. Read through 'option attributes' to get an explanation of what you've listed, and then 'actions' to see what I've mentioned above

这篇关于使用 args 和选项编写自定义管理命令 - 所需字段说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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