3

我正在尝试使用call_command从 URL 下载数据,并且想知道如何从代码中调用它。

我在我的代码中声明了如下选项列表:

option_list = BaseCommand.option_list + (
        make_option('--url', default=None, dest='url', help=_(u'Specifies the full url of the json data to download.')),
        make_option('--username', default=None, dest='username', help=_(u'Login of the person doing the download.')),
        make_option('--password', default=None, dest='password', help=_(u'Password of the person doing the download.')),
        make_option('--file', default=None, dest='file', help=_(u'File name of the json data to download in gzip-compressed-data format')),
    )

我从命令行按如下方式使用它:

./manage.py download --url=http://some-link.com/download/ --username=admin --password=admin

到目前为止,我有以下内容:

call_command('download')

如何传递其余的参数/参数?

4

1 回答 1

4

只需将它们作为关键字参数传递:

call_command('download', 
             url="http://some-link.com/download/", 
             username="admin",
             password="admin")

关键字参数应反映dest自定义管理命令参数的值。


命令示例flush

--initial-datacall_command()可以通过传递load_initial_data关键字参数来设置命令行参数:

call_command('flush', load_initial_data=False)

这是因为它是--initial-data的参数目的地:

parser.add_argument('--no-initial-data', action='store_false',
    dest='load_initial_data', default=True,
    help='Tells Django not to load any initial data after database synchronization.')
于 2014-08-04T06:45:22.920 回答