Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 898
AdamY Avatar asked Dec 30 '16 18:12

AdamY


1 Answers

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):
like image 189
AdamY Avatar answered Oct 17 '22 05:10

AdamY