You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class TokenObtainPairSerializer(TokenObtainSerializer):
@classmethod
def get_token(cls, user):
return RefreshToken.for_user(user)
def validate(self, attrs):
data = super(TokenObtainPairSerializer, self).validate(attrs)
refresh = self.get_token(self.user)
data['refresh'] = text_type(refresh)
data['access'] = text_type(refresh.access_token)
return data
Why refresh and access fields are added in validate method? This method is supposed to validate data returned by to_internal_value, why not add those to keys in to_internal_value?
In my case, I had to override TokenObtainPairSerializer in the following way:
class CustomTokenObtainPairSerializer(CustomTokenObtainSerializer, TokenObtainPairSerializer):
access = serializers.CharField(read_only=True)
refresh = serializers.CharField(read_only=True)
def validate(self, attrs):
data = super().validate(attrs)
refresh = self.get_token(self.user)
data['refresh'] = text_type(refresh)
data['access'] = text_type(refresh.access_token)
return data
I had to explicitly declare these two fields under class statement, otherwise I have been receiving:
KeyError: "Got KeyError when attempting to get a value for field email on serializer CustomTokenObtainPairSerializer.\nThe serializer field might be named incorrectly and not match any attribute or key on the dict instance.\nOriginal exception text was: 'email'."
The text was updated successfully, but these errors were encountered:
Just a question, why did you base your serializer from two classes, including your custom CustomTokenObtainSerializer?
In my case I inherited from TokenObtainPairSerializer to create my serializer and wrote validate method to send signal user_logged_in. Everything works as expected.
Let's look at:
Why
refresh
andaccess
fields are added invalidate
method? This method is supposed to validate data returned byto_internal_value
, why not add those to keys into_internal_value
?In my case, I had to override
TokenObtainPairSerializer
in the following way:I had to explicitly declare these two fields under
class
statement, otherwise I have been receiving:The text was updated successfully, but these errors were encountered: