Fedya Grab
Fedya Grab

Reputation: 46

Token key name django-rest-auth

I'm using django-rest-auth==0.9.3 in my Django project (I'm making REST API for mobile application) and i get this problem: After authentication which is used on {base_url}/api/users/login/, API gives me the user's token in a JSON, but the key value of this token is 'key':

{
   "key": "1eca799e88fd76bea3b33c53c33d58e4940bc7b8"
}

I want it to be "token". Does anyone know special properties for that or how to customize my TokenSerializer or any other solutions?

Upvotes: 0

Views: 649

Answers (2)

aman kumar
aman kumar

Reputation: 3156

you can write the custom serializer

class TokenSerializer(serializers.ModelSerializer):
    """
    serializer for getting the user token for authentication
    """
    token = serializers.CharField(source='key')

    class Meta:
        model = Token

use this serializer to return the response

Upvotes: 0

JPG
JPG

Reputation: 88539

As @Bear Brown mentioned in comment, use a custom serializer class in your code,

from rest_auth.models import TokenModel
from rest_framework import serializers


class MyCustomTokenSerializer(serializers.ModelSerializer):
    token = serializers.CharField(source='key')

    class Meta:
        model = TokenModel
        fields = ('token',)

and
add the path to the serializer in settings.py as,

REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'path.to.custom.MyCustomTokenSerializer',
}

Upvotes: 1

Related Questions