我还如何通过令牌"Key"获取用户数据?在django rest_auth登录端点的HTTP响应中? [英] How do i also get user data with token "Key" in http response to django rest_auth login endpoint?

查看:104
本文介绍了我还如何通过令牌"Key"获取用户数据?在django rest_auth登录端点的HTTP响应中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与django一起工作对我来说是新手.我使用扩展AbstractUser的自定义用户模型开发了使用Django Rest-Auth进行用户注册,登录和注销的API.

I am very new to working with django. i have developed an API for user Registration, Login and Logout with Django Rest-Auth Using Custom User Model extending AbstractUser.

当我使用凭据在Login API端点上调用POST时,我得到令牌作为响应,请检查以下屏幕截图.

When i call POST on Login API endpoint using credential, i get token in response, Please check below screenshot.

在登录API端点上发布

我还需要获取登录用户"数据.我该怎么做!?

I also need to get Logged in User data. how do i do that!?

任何帮助都可以.TIA.

Any help would do. TIA.

推荐答案

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

You can override the default TokenSerializer with a custom serializer that will include users.

在文件中说出yourapp/model.py

in a file say yourapp/model.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配置覆盖默认类

and in your app settings use rest-auth configuration to override the default class

yourapp/settings.py

yourapp/settings.py

.
.
.
.

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

这篇关于我还如何通过令牌"Key"获取用户数据?在django rest_auth登录端点的HTTP响应中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆