小编mit*_*ril的帖子

使用args和选项编写自定义管理命令 - 解释所需的字段

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

我从django文档中获得了很多帮助.也得到了重要指针这里了解如何传递多个参数,并在这里了解如何有选择.

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()
    # ...
Run Code Online (Sandbox Code Playgroud)

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

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 : …
Run Code Online (Sandbox Code Playgroud)

django command

31
推荐指数
2
解决办法
3万
查看次数

标签 统计

command ×1

django ×1