Search code examples
djangodjango-rest-frameworkdjango-rest-auth

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


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

Solution

  • 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