Is there any way to change view of Django-rest-auth of login?

安稳与你 提交于 2019-12-12 12:13:32

问题


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',)

回答1:


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

and change your urls.py as

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

              ] 

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



来源:https://stackoverflow.com/questions/52110466/is-there-any-way-to-change-view-of-django-rest-auth-of-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!