• <tfoot id='G5ShV'></tfoot>
      • <bdo id='G5ShV'></bdo><ul id='G5ShV'></ul>

    1. <legend id='G5ShV'><style id='G5ShV'><dir id='G5ShV'><q id='G5ShV'></q></dir></style></legend>

        <i id='G5ShV'><tr id='G5ShV'><dt id='G5ShV'><q id='G5ShV'><span id='G5ShV'><b id='G5ShV'><form id='G5ShV'><ins id='G5ShV'></ins><ul id='G5ShV'></ul><sub id='G5ShV'></sub></form><legend id='G5ShV'></legend><bdo id='G5ShV'><pre id='G5ShV'><center id='G5ShV'></center></pre></bdo></b><th id='G5ShV'></th></span></q></dt></tr></i><div id='G5ShV'><tfoot id='G5ShV'></tfoot><dl id='G5ShV'><fieldset id='G5ShV'></fieldset></dl></div>

        <small id='G5ShV'></small><noframes id='G5ShV'>

        如何使 Django 自定义管理命令参数不需要?

        时间:2023-10-19
        <tfoot id='ZrAa4'></tfoot>

            <tbody id='ZrAa4'></tbody>
            <legend id='ZrAa4'><style id='ZrAa4'><dir id='ZrAa4'><q id='ZrAa4'></q></dir></style></legend>
            1. <i id='ZrAa4'><tr id='ZrAa4'><dt id='ZrAa4'><q id='ZrAa4'><span id='ZrAa4'><b id='ZrAa4'><form id='ZrAa4'><ins id='ZrAa4'></ins><ul id='ZrAa4'></ul><sub id='ZrAa4'></sub></form><legend id='ZrAa4'></legend><bdo id='ZrAa4'><pre id='ZrAa4'><center id='ZrAa4'></center></pre></bdo></b><th id='ZrAa4'></th></span></q></dt></tr></i><div id='ZrAa4'><tfoot id='ZrAa4'></tfoot><dl id='ZrAa4'><fieldset id='ZrAa4'></fieldset></dl></div>

              <small id='ZrAa4'></small><noframes id='ZrAa4'>

                  <bdo id='ZrAa4'></bdo><ul id='ZrAa4'></ul>
                  本文介绍了如何使 Django 自定义管理命令参数不需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 django 中编写一个自定义管理命令,如下所示-

                  I am trying to write a custom management command in django like below-

                  class Command(BaseCommand):
                  
                      def add_arguments(self, parser):
                          parser.add_argument('delay', type=int)
                  
                      def handle(self, *args, **options):
                          delay = options.get('delay', None)
                          print delay
                  

                  现在,当我运行 python manage.py mycommand 12 时,它会在控制台上打印 12.这很好.

                  Now when I am running python manage.py mycommand 12 it is printing 12 on console. Which is fine.

                  现在,如果我尝试运行 python manage.py mycommand 然后我想要,该命令默认在控制台上打印 21.但它给了我这样的东西-

                  Now if I try to run python manage.py mycommand then I want that, the command prints 21 on console by default. But it is giving me something like this-

                  usage: manage.py mycommand [-h] [--version]
                                             [-v {0,1,2,3}]
                                             [--settings SETTINGS]
                                             [--pythonpath PYTHONPATH]
                                             [--traceback]
                                             [--no-color]
                                             delay
                  

                  那么现在,如果没有给出值,我应该如何使命令参数不需要"并取默认值?

                  So now, how should I make the command argument "not required" and take a default value if value is not given?

                  推荐答案

                  文档 建议:

                  对于 nargs 等于 ?* 的位置参数,当不存在命令行参数时使用 default 值.

                  For positional arguments with nargs equal to ? or *, the default value is used when no command-line argument was present.

                  所以下面应该可以解决问题(如果提供,它将返回值,否则返回默认值):

                  So following should do the trick (it will return value if provided or default value otherwise):

                  parser.add_argument('delay', type=int, nargs='?', default=21)
                  

                  用法:

                  $ ./manage.py mycommand
                  21
                  $ ./manage.py mycommand 4
                  4
                  

                  这篇关于如何使 Django 自定义管理命令参数不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python/python3 在命令提示符下执行,但运行不正确 下一篇:使用 south 重构具有继承的 Django 模型

                  相关文章

                  <i id='0JhNW'><tr id='0JhNW'><dt id='0JhNW'><q id='0JhNW'><span id='0JhNW'><b id='0JhNW'><form id='0JhNW'><ins id='0JhNW'></ins><ul id='0JhNW'></ul><sub id='0JhNW'></sub></form><legend id='0JhNW'></legend><bdo id='0JhNW'><pre id='0JhNW'><center id='0JhNW'></center></pre></bdo></b><th id='0JhNW'></th></span></q></dt></tr></i><div id='0JhNW'><tfoot id='0JhNW'></tfoot><dl id='0JhNW'><fieldset id='0JhNW'></fieldset></dl></div>

                  1. <small id='0JhNW'></small><noframes id='0JhNW'>

                    <legend id='0JhNW'><style id='0JhNW'><dir id='0JhNW'><q id='0JhNW'></q></dir></style></legend>
                      <bdo id='0JhNW'></bdo><ul id='0JhNW'></ul>
                    1. <tfoot id='0JhNW'></tfoot>