首页 > 解决方案 > @classmethod 错误:TypeError:__call__() 接受 2 个位置参数,但给出了 3 个

问题描述

我有一个这样的模型/类:

class MyModel(TimeStampedModel):
    some_field = models.CharField()

    @classmethod
    def my_class_method(cls, value, other_value):
        print(value)

但是,当我从另一个角度这样做时:

value = "Test"
MyModel.my_class_method(value, other_value)

我收到一个错误:TypeError: __call__() takes 2 positional arguments but 3 were given

我觉得我快疯了。

我这样做的模型有:

class Meta:
    abstract = True

编辑:完整回溯(使用我的真实函数名称):

Traceback (most recent call last):
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\whitenoise\base.py", line 66, in __call__
    return self.application(environ, start_response)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\wsgi.py", line 146, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 81, in get_response
    response = self._middleware_chain(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\whitenoise\middleware.py", line 25, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
    response = self.get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Cole\PycharmProjects\webapp\config\wsgi.py", line 27, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Cole\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Cole\PycharmProjects\webapp\vendor_app\apps\users\decorators.py", line 14, in wrap
    return function(request, *args, **kwargs)
  File "C:\Users\Cole\PycharmProjects\webapp\vendor_app\apps\core\locations\views.py", line 56, in location_detail_view
    Location.create_or_update_location(form, formset)
TypeError: __call__() takes 2 positional arguments but 3 were given

标签: djangopython-3.xdjango-model-utils

解决方案


该问题似乎是由在事务原子装饰器中包装类方法引起的:

@transaction.atomic
@classmethod
def my_function(cls, **kwargs):
    ... my function

显然这似乎不起作用。


推荐阅读