django-rest-knox in production: "TypeError: create() takes 1 positional argument but 2 were given"

django-rest-knox in production: "TypeError: create() takes 1 positional argument but 2 were given"

我正在使用 Knox 进行令牌身份验证。它在开发过程中完美运行,但现在我尝试在 PythonAnywhere 上部署时,我在标题中收到错误:“TypeError: create() takes 1 positional argument but 2 were given”。

这是完整的回溯:

2020-09-10 13:42:03,788: Internal Server Error: /api/auth/login
Traceback (most recent call last):
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/<user>/<website>/registration/views.py", line 38, in post
    "token": AuthToken.objects.create(user)[1]
  File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
TypeError: create() takes 1 positional argument but 2 were given

我的registration.views(错误发生在second-to-last行):

class RegisterDetail(generics.GenericAPIView):
    serializer_class = RegisterSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })

class LoginDetail(generics.GenericAPIView):
    serializer_class = LoginSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })

Queryset.Create 只接受关键字参数。该错误不会在身份验证时发生。它正在创建一个令牌,这意味着您可以通过 username/password 自动创建令牌或续订或登录,然后创建一个令牌。

由于您的开发用户可能已经拥有令牌,因此您不会在开发过程中看到该错误。