The django-rest-auth
returns a token after successful authentication using rest_auth.serializers.TokenSerializer
. How can I override this serializer or add my own so that I can get the user info like username, instead of token key after successful authentication?
I solved the problem by defining a custom serializer.
from django.contrib.auth.models import User
from rest_framework import serializers
from rest_auth.models import TokenModel
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'email')
class TokenSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = TokenModel
fields = ('key', 'user')
You can also use the depth
option to easily generate nested representations, but in that case, you will receive the password
field as well, which is not expected and intended.