Django命令抛出TypeError:handle()得到一个意外的关键字参数

时间:2016-12-30 18:50:45

标签: django python-3.5

我正在使用Django 1.10.4和Python 3.52。当我尝试通过python manage.py my_command运行Django命令时,我收到以下错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
TypeError: handle() got an unexpected keyword argument 'verbosity'

我可以运行本地django服务器并与管理页面进行交互。包含该命令的应用程序位于我的settings.py文件中。

以下是django命令的内容:

from django.core.management import BaseCommand
from my_module import MyClass


class Command(BaseCommand):
    def handle(self):
        my_class = MyClass()
        my_class.my_method()

发生错误时,options字典包含{'verbosity': 1, 'no_color': False, 'settings': None, 'pythonpath': None, 'traceback': False}。根据字典no_color的随机顺序,traceback,其他人将抛出相同的TypeError。经过一天的谷歌搜索后,我仍然无法弄清楚问题是什么。有没有人见过这个?

2 个答案:

答案 0 :(得分:13)

经过大量的谷歌搜索和拔出头发后,问题是handle()的参数数量不正确。

此:

    def handle(self):

应该是:

    def handle(self, *args, **options):

答案 1 :(得分:-1)

如果您的命令不需要参数,请尝试subclass of BaseCommand

NoArgsCommand.handle_noargs(**options)