Djangorestframework-simplejwt: Issue when Authentication fails: __init__() takes from 1 to 2 positional arguments but 3 were given

Created on 2 Aug 2019  Â·  4Comments  Â·  Source: jazzband/djangorestframework-simplejwt

I have the following in my views.py

from rest_framework_simplejwt.views import TokenObtainPairView
from .serializers import CustomTokenObtainPairSerializer

class CustomTokenObtainPairView(TokenObtainPairView):
    serializer_class = CustomTokenObtainPairSerializer

And in my serializers.py

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from backend.users.serializers import UserSerializer

class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        tokens = super(CustomTokenObtainPairSerializer, self).validate(attrs)

        data = {
            "tokens": tokens,
            "user": UserSerializer(self.user).data
        }
        return data

When Authentication is successful, data is returned with no issues. But when Authentication fails, instead of getting a JSON with the error message, I get the following Django Exception:

__init__() takes from 1 to 2 positional arguments but 3 were given

Traceback:
```
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner

  1. response = get_response(request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response

  1. response = self._get_response(request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response

  1. response = self.process_exception_by_middleware(e, request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response

  1. response = wrapped_callback(request, callback_args, *callback_kwargs)

File "/usr/lib/python3.5/contextlib.py" in inner

  1. return func(args, *kwds)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view

  1. return view_func(args, *kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/generic/base.py" in view

  1. return self.dispatch(request, args, *kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch

  1. response = self.handle_exception(exc)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch

  1. response = handler(request, args, *kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/views.py" in post

  1. serializer.is_valid(raise_exception=True)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in is_valid

  1. self._validated_data = self.run_validation(self.initial_data)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in run_validation

  1. value = self.validate(value)

File "/vagrant/Devel/sample_app/sample_app/backend/apps/api/auth/serializers.py" in validate

  1. tokens = super(CustomTokenObtainPairSerializer, self).validate(attrs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate

  1. data = super().validate(attrs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate

  1. 'no_active_account',

Exception Type: TypeError at /api/auth/token/obtain/
Exception Value: __init__() takes from 1 to 2 positional arguments but 3 were given

Most helpful comment

@Andrew-Chen-Wang @naelmusleh @haristimuno-omnibnk

🎉 🎊 Fixed it. 🎉 🎊 I was unnecessarily adding the extended, customized serializer to my settings.
Deleting it from settings.

Now my extending process looks like this:
serializer > view > url

No need to change anything in settings.

All 4 comments

Did you solve this issuer?

Reviving issue, any ideas?

@naelmusleh @haristimuno-omnibnk @hvitis I’m not sure where the problem could lie to be honest IN GENERAL. But if you take a look at the original def validate, it already returns something... so having token = super() doesn’t really make (at first glance, it seemed like an infinite loop, but I guess not). You’re getting this because you’ve supplied validate with super().validate(attrs) which in turn makes the serializer’s validate method use self.validate(attrs, attrs) which is giving you this error.

So instead of using super(), rewrite the entire validate method.

Another theory which is not as good since the above is probably the correct identification of the problem: Based on traceback error, you’re missing the whole “not authenticated part” as in the default:

if self.user is None or not self.user.is_active:
            raise exceptions.AuthenticationFailed(
                self.error_messages['no_active_account'],
                'no_active_account',
            )

        return {}

If you take a look at what the original problem was, we have a default “no_account_account” error that should’ve been raised, but I suppose wasn’t based on the original poster’s serializer... which shouldn’t have touched the validation part to be honest.

@Andrew-Chen-Wang @naelmusleh @haristimuno-omnibnk

🎉 🎊 Fixed it. 🎉 🎊 I was unnecessarily adding the extended, customized serializer to my settings.
Deleting it from settings.

Now my extending process looks like this:
serializer > view > url

No need to change anything in settings.

Was this page helpful?
0 / 5 - 0 ratings