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

308 views Asked by At

I am doing token authentication with Knox. It has worked perfectly through development, but now that I try to deploy on PythonAnywhere, I get the error in the title: "TypeError: create() takes 1 positional argument but 2 were given".

This is the full traceback:

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

My registration.views (error occurs in second-to-last line):

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]
        })
1

There are 1 answers

0
AudioBubble On BEST ANSWER

Queryset.Create only accepts keyword arguments. The error doesn't occur at authentication. It is creating a token, which means you have some automatic token creation perhaps renewal or sign in through username/password that then creates a token.

Since your development user likely already has a token, you will not see the error during development.