我试图用django编写一个自定义管理命令,如下所示-
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。这很好。
现在,如果我尝试运行python manage.py mycommand
,那么我需要这样做,默认情况下,该命令在控制台上打印21。但它给了我这样的东西-
usage: manage.py mycommand [-h] [--version]
[-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH]
[--traceback]
[--no-color]
delay
所以,现在,我应该如何使命令参数“不需要”,并采取默认值,如果值没有给定?
来自documentation的一个食谱建议:
因此,下面应该做的诀窍(它将返回值,如果提供或默认值,否则):
用法:
对于可选的关键字参数,可以使用破折号语法:
使用:
文件:
https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/#s-accepting-optional-arguments
简单解释:
https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html#handling-arguments
相关问题 更多 >
编程相关推荐