Привет всем, кому нужно сделать три apis для входа в систему, регистрации и выхода из системы ... Могу ли я сделать три api с этим одним сериализатором ...?
serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from rest_framework.authtoken.models import Token
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['id', 'username', 'email', 'password']
extra_kwargs = {'password': {'write_only': True, 'required': True}}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
Token.objects.create(user=user)
return user
views.py
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from .serializers import UserSerializer
# from rest_framework.authentication import TokenAuthentication
# from rest_framework.permissions import IsAuthenticated
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# authentication_classes = (TokenAuthentication,)
# permission_classes = (IsAuthenticated, )
urls.py
from django.urls import include, path
from rest_framework import routers
from .views import UserViewSet
from rest_framework.authtoken.views import ObtainAuthToken
router = routers.DefaultRouter()
router.register(r'registration', UserViewSet)
urlpatterns = [
path('', include(router.urls)),
path('signIn/', ObtainAuthToken.as_view())
]