ian
ian

Reputation: 111

DRF 'ObtainAuthToken' object has no attribute 'request'

I am getting an error every time the user login. The function should show the Token associated with the user, but I am getting an error message 'ObtainAuthToken' object that has no attribute 'request'.

Views.py

from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.authtoken.views import ObtainAuthToken

class LoginView(generics.CreateAPIView):
    serializer_class = AuthTokenSerializer
    
    def create(self, request):
        return ObtainAuthToken().post(request)

Installed App

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'core',
    
    'rest_framework',
    'rest_framework.authtoken',
]

Upvotes: 0

Views: 887

Answers (1)

paras chauhan
paras chauhan

Reputation: 787

class LoginView(generics.CreateAPIView):
    serializer_class = AuthTokenSerializer

    def create(self, request):
        return ObtainAuthToken().as_view()(request=request._request)

This would be work for you

Upvotes: 5

Related Questions