将负值作为参数传递给django中的自定义管理命令

时间:2021-09-11 05:11:01

Like many others, I've been learning web development on django through building a test app. I've the basic models set up. I've populated a few of the tables with the absolute minimum data needed for further testing though using fixtures.

像许多其他人一样,我一直在通过构建测试应用程序来学习django上的web开发。我已经设置了基本模型。我已经填充了一些表格,其中包含了使用灯具进行进一步测试所需的绝对最小数据。

Now for a different table, I want to create data tuples through a custom management command which takes the required arguments. If this works as expected, I'll save the created data to the database by adding the --save option.

现在,对于不同的表,我想通过自定义管理命令创建数据元组,该命令采用所需的参数。如果这按预期工作,我将通过添加--save选项将创建的数据保存到数据库。

The syntax of the command is like this

命令的语法是这样的

create_raw_data owner_id temperature [--save]

where owner_id is required and temperature (in C) is optional. Within the Handle method, I'm using factory boy to create the raw_data with the given arguments etc.

其中owner_id是必需的,温度(C)是可选的。在Handle方法中,我使用工厂boy来创建带有给定参数的raw_data等。

I did have some issues but searching on SO, google, django docs etc, I've got the command working fine.

我确实有一些问题,但搜索SO,谷歌,Django文档等,我已经得到了正常的命令。

EXCEPT when I input a negative temperature...

当我输入负温度时...

Then I get the following error

然后我收到以下错误

Usage: C:\test\manage.py create_raw_data [options] 

Creates a RawData object. Usage:  create_raw_data owner_id temperature [--save]

C:\test\manage.py: error: no such option: -5

The code I have for parsing the args is like this

我解析args的代码是这样的

for index, item in enumerate(args):
    if index == 0:
        owner_id = int(item)
    else index == 1:
        temp = int(item)

I put a print(args) as the 1st line inside Handle but it seems the control is not even reaching here.

我把一个打印(args)作为Handle中的第一行,但似乎控件甚至没有到达这里。

I'm not sure what is wrong... please help...

我不确定有什么问题...请帮忙......

Thanks a lot

非常感谢

1 个解决方案

#1


1  

got the issue fixed so providing an answer to others who may come across this.

解决了这个问题,以便为可能遇到此问题的其他人提供答案。

The issue was with parse_args method of optparse. I've read in a number of places that though optparse is deprecated and instead argparse is recommended, django recommends using optparse since that is what it uses. Long story short, the link at link suggested a few alternatives and using create_raw_data 1 -- -5 works as expected. So I did get a workaround. Thanks.

问题在于使用optparse的parse_args方法。我已经在很多地方读到虽然不推荐使用optparse而是建议使用argparse,但是django建议使用optparse,因为它是它的用途。长话短说,链接链接提示了一些替代方案,并使用create_raw_data 1 - -5按预期工作。所以我确实得到了解决方法。谢谢。

#1


1  

got the issue fixed so providing an answer to others who may come across this.

解决了这个问题,以便为可能遇到此问题的其他人提供答案。

The issue was with parse_args method of optparse. I've read in a number of places that though optparse is deprecated and instead argparse is recommended, django recommends using optparse since that is what it uses. Long story short, the link at link suggested a few alternatives and using create_raw_data 1 -- -5 works as expected. So I did get a workaround. Thanks.

问题在于使用optparse的parse_args方法。我已经在很多地方读到虽然不推荐使用optparse而是建议使用argparse,但是django建议使用optparse,因为它是它的用途。长话短说,链接链接提示了一些替代方案,并使用create_raw_data 1 - -5按预期工作。所以我确实得到了解决方法。谢谢。