Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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',)
like image 638
Kashyap Avatar asked Aug 31 '18 07:08

Kashyap


People also ask

What is basic authentication in Django REST framework?

Basic Authentication in Django REST Framework uses HTTP Basic Authentication. It is generally appropriate for testing. The REST framework will attempt to authenticate the Basic Authentication class and set the returned values to request. user and request.


1 Answers

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

like image 141
JPG Avatar answered Sep 28 '22 06:09

JPG