在Django中,我正在制作一个名为manage.py
的{{1}}命令。
这是我到目前为止some_command
所拥有的内容。
management/commands/some_command.py
这给了我错误:class Command(BaseCommand):
def add_arguments(self,parser):
parser.add_argument(
"--fields",
dest="fields",
required=True,
help="Fields to query; Enter in pipe-delimited format",
)
def handle(self, *args, **options):
Model= apps.get_model(app_label="my_app", model_name="MyModelName")
print Model.objects.values(tpl(fields.split('|')))
。
选项AttributeError: 'tuple' object has no attribute 'split'
是我想查询的一串列,由管道字符分隔。所以它看起来像" col1 | col2"。如何通过此自定义Django命令行进程查询这些字段?
答案 0 :(得分:1)
您需要在此处使用参数unpacking。将您的最后一行更改为
print Model.objects.values(*options['fields'].split('|'))
请注意,您需要正确引用命令行争论,因为管道字符在终端中具有特殊含义。
python manage.py yourcommand --fields =" col1 | col2"