Django command throws TypeError: handle() got an unexpected keyword argument

做~自己de王妃 提交于 2021-01-02 05:56:18

问题


I'm using Django 1.10.4 and Python 3.52. When I try to run a Django command via python manage.py my_command I get the following error:

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'

I can run a local django server and interact with the admin pages. The app that contains that command is in my settings.py file.

Below is the contents of the django command:

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


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

At the time of error, the options dictionary contains {'verbosity': 1, 'no_color': False, 'settings': None, 'pythonpath': None, 'traceback': False}. Depending on the random ordering of the dictionary no_color, traceback, and the others will throw the same TypeError. After a day of googling I still can't figure out what the issue is. Has anyone seen this before?


回答1:


After lots of googling and pulling my hair out, the issue was an incorrect number of arguments to handle().

This:

    def handle(self):

Should be:

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



回答2:


If your command needs no arguments, try a subclass of BaseCommand

NoArgsCommand.handle_noargs(**options)


来源:https://stackoverflow.com/questions/41401202/django-command-throws-typeerror-handle-got-an-unexpected-keyword-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!