Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 登录“rest auth”后,如何返回更多信息?_Python_Django_Django Rest Auth - Fatal编程技术网

Python 登录“rest auth”后,如何返回更多信息?

Python 登录“rest auth”后,如何返回更多信息?,python,django,django-rest-auth,Python,Django,Django Rest Auth,我在django项目中使用了django rest auth 登录rest auth/login/后,如何返回更多信息 在restauth/login/中,当我登录用户时,它返回一个键 我还想返回用户的信息,我怎样才能得到这个 最后,我得到了我的解决方案: class TokenSerializer(serializers.ModelSerializer): """ Serializer for Token model. """ user = UserInfoS

我在django项目中使用了
django rest auth

登录
rest auth/login/
后,如何返回更多信息

restauth/login/
中,当我登录用户时,它返回一个


我还想返回用户的信息,我怎样才能得到这个

最后,我得到了我的解决方案:

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\u序列化程序
,如下所示:

REST_AUTH_SERIALIZERS = {
    ...
    'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
}
现在我得到了我需要的数据:

请参考此

您可以使用包含用户的自定义序列化程序覆盖默认的
令牌序列化程序

在文件中说yourapp/serializers.py

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', )
在应用程序设置中,使用rest auth配置覆盖默认类

yourapp/settings.py

REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'yourapp.serializers.CustomTokenSerializer' # import path to CustomTokenSerializer defined above.
}

使用request.user或打印并检查。@AnupYadav我使用第三个
rest-auth
lib,没有适合我的视图。视图由
rest auth
控制。您可以使用获取所有信息,也可以为每个OAuth为google OAuth为其他服务提供商构建自定义库。您可以使用自己的(自定义)此库对我来说也不错