最后,我得到了我的解决方案:
class TokenSerializer(serializers.ModelSerializer):
"""
Serializer for Token model.
"""
user = UserInfoSerializer(many=False, read_only=True) # this is add by myself.
class Meta:
model = TokenModel
fields = ('key', 'user') # there I add the `user` field ( this is my need data ).
在项目的settings.py
文件中加入以下内容:TOKEN_SERIALIZER
。REST_AUTH_SERIALIZERS = {
...
'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
}
现在我已经得到了所需的数据:
TokenSerializer
,该序列化程序将包括用户。from django.conf import settings
from rest_framework import serializers
from rest_auth.models import TokenModel
from rest_auth.utils import import_callable
from rest_auth.serializers import UserDetailsSerializer as DefaultUserDetailsSerializer
# This is to allow you to override the UserDetailsSerializer at any time.
# If you're sure you won't, you can skip this and use DefaultUserDetailsSerializer directly
rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
UserDetailsSerializer = import_callable(
rest_auth_serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer)
)
class CustomTokenSerializer(serializers.ModelSerializer):
user = UserDetailsSerializer(read_only=True)
class Meta:
model = TokenModel
fields = ('key', 'user', )
yourapp/settings.py
REST_AUTH_SERIALIZERS = {
'TOKEN_SERIALIZER': 'yourapp.serializers.CustomTokenSerializer' # import path to CustomTokenSerializer defined above.
}
rest-auth
库,对我来说没有视图。视图由rest-auth
控制。 - aircraft