我在Django项目中使用django-rest-auth==0.9.3
(正在为移动应用程序制作REST API),但出现了以下问题:
经过{base_url} / api / users / login /上使用的身份验证后,API会以JSON的形式给我用户的令牌,但此令牌的键值为'key':
{
"key": "1eca799e88fd76bea3b33c53c33d58e4940bc7b8"
}
我希望它是“令牌”。 有谁知道特殊属性或如何自定义我的TokenSerializer或任何其他解决方案?
答案 0 :(得分:0)
您可以编写自定义序列化器
class TokenSerializer(serializers.ModelSerializer):
"""
serializer for getting the user token for authentication
"""
token = serializers.CharField(source='key')
class Meta:
model = Token
使用此序列化器返回响应
答案 1 :(得分:0)
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',
}