1
我寫了一個管理命令:Django的管理命令無法返回字典
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("member_id", nargs="+", type=str)
def handle(self, *args, **options):
return other_function(options["member_id"][0])
調用導入函數:
def other_function(identifier):
return {"foo": "bar"}
當我打電話從shell其它功能是否能夠正常工作;但是,使用管理命令時,我得到:
File "/Volumes/www/bin/../apps/manage.py", line 61, in <module>
execute_from_command_line(sys.argv)
File "/Volumes/www/src/django/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Volumes/www/src/django/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Volumes/www/src/django/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/Volumes/www/lib/python2.7/site-packages/raven/contrib/django/management/__init__.py", line 41, in new_execute
return original_func(self, *args, **kwargs)
File "/Volumes/www/src/django/django/core/management/base.py", line 454, in execute
self.stdout.write(output)
File "/Volumes/www/src/django/django/core/management/base.py", line 111, in write
if ending and not msg.endswith(ending):
AttributeError: 'dict' object has no attribute 'endswith'
管理命令是否只能返回字符串?該文件似乎並沒有這麼說,但如果我將handle
函數更改爲return "foo"
它可以工作。但那似乎很愚蠢。
doc確實說它必須返回一個unicode字符串:https://docs.djangoproject.com/en/1.9/howto/custom-management -commands /#django.core.management.BaseCommand.handle – Fabricator