[Fixed]-Django: TokenAuthentication and AbstractBaseUser Errors

1๐Ÿ‘

โœ…

You are not passing the user instance to Token.
Can you try this:

In routers.py

from rest_framework import routers
from .viewsets import UserSeralizerViewSets
router = routers.DefaultRouter()
router.register(r'users', UserSeralizerViewSets)

In urls.py

from .routers import router
urlpatterns = [
    url(r'^', include(router.urls)),
]

In viewsets.py

class UserSeralizerViewSets(viewsets.ModelViewSet):
    serializer_class = UserSeralizer
    queryset = Users.objects.all()

   def create(self, request, *args, **kwargs):
       data = {}
       serializer = self.get_serializer(data=request.data)
       serializer.is_valid(raise_exception=True)
       user = serializer.save()
       token, created = Token.objects.get_or_create(user=user)
       data['token'] = token.key
       return Response(data, status=status.HTTP_201_CREATED)
๐Ÿ‘คSeenu S

0๐Ÿ‘

The answer of seenu s is well but i use this line in settings.py and worked .

     AUTH_USER_MODEL = 'users.Users'

Users.Users is (myappname.mymodelname)
I find this on Django doc.

๐Ÿ‘คSoheil Tayyeb

Leave a comment