有什么办法可以更改登录的Django-rest-auth的视图吗? [英] Is there any way to change view of Django-rest-auth of login?

查看:95
本文介绍了有什么办法可以更改登录的Django-rest-auth的视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Django-rest-auth创建了rest API,登录后会返回密钥和一些用户信息,但是我需要添加一些状态,例如成功和消息等。有什么方法可以覆盖django-rest-auth的登录视图?

I've created rest APIs using Django-rest-auth, in login, it's returning key and some user info, But I need to add some status like success and message and some other things. Is there any way to override view of django-rest-auth for login?

class TokenSerializer(serializers.ModelSerializer):
    user = UserSerializer(many=False, read_only=True)  # this is add by myself.
    device = DeviceSerializer(many=True, read_only=True)

    class Meta:
        model = TokenModel
        fields = ('key', 'user', 'device',)


推荐答案

创建 自定义视图类 并使用它

Create a custom view class and use it

from rest_auth.views import LoginView


class CustomLoginView(LoginView):
    def get_response(self):
        orginal_response = super().get_response()
        mydata = {"message": "some message", "status": "success"}
        orginal_response.data.update(mydata)
        return orginal_response

并将您的 urls.py 更改为

urlpatterns = [
                  url(r'custom/login/', CustomLoginView.as_view(), name='my_custom_login')

              ] 

现在您应该使用端点 / custom / login / 代替 / rest-auth / login

now you should use the endpoint /custom/login/ instead of /rest-auth/login

这篇关于有什么办法可以更改登录的Django-rest-auth的视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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