Home Ask Login Register

Developers Planet

Your answer is one click away!

rrmo February 2016

Override the authToken views in Django Rest

I am using the token based Authentication in Django and need to add User object in addition to token being returned.

How do I override this class view ? Where do I need add this class and make the changes ? Currently this is found in the rest_framework package and I don't want to modify the library .

from rest_framework import parsers, renderers
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
from rest_framework.views import APIView

class ObtainAuthToken(APIView):
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)
    serializer_class = AuthTokenSerializer
    print "dasdsa"

    def post(self, request):
        serializer = self.serializer_class(data=request.data)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)
        return Response({'token': token.key})

obtain_auth_token = ObtainAuthToken.as_view()


djq February 2016

I wanted to override some default CRSF functionality and used the following approach:

from rest_framework.authentication import SessionAuthentication

class SessionCsrfExemptAuthentication(SessionAuthentication):

    def enforce_csrf(self, request):
        # Do not perform a csrf check
        return False

Then in my settings file I referenced it in the following way:


This allowed me to import the existing functionality, override it and reference it in the settings file. I think you can use a similar approach here.

Moe Far February 2016

You should extend your CustomClass from AuthToken, the route default url to your CustomClass:

from rest_framework_jwt.views import ObtainJSONWebToken

class JSONWebTokenAPIOverride(ObtainJSONWebToken):
    Override JWT
    def post(self, request):
        # Do whatever you want

Then in your urls.py:


I hope it helps

Post Status

Asked in February 2016
Viewed 2,307 times
Voted 13
Answered 2 times


Leave an answer

Quote of the day: live life