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
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
File "/usr/lib/python3.5/contextlib.py" in inner
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/generic/base.py" in view
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/views.py" in post
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in is_valid
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in run_validation
File "/vagrant/Devel/sample_app/sample_app/backend/apps/api/auth/serializers.py" in validate
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate
File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate
Exception Type: TypeError at /api/auth/token/obtain/
Exception Value: __init__() takes from 1 to 2 positional arguments but 3 were given
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.
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.