cheslijones
cheslijones

Reputation: 9194

Possible to have multiple serializers per view using Django Rest Framework

As I'm working, my ./views.py is becoming really competitive and I'm wondering if there is a way to refactor it to be DRY. I think in order to do so, I would need the ability to use specify multiple serializers per view.

data would need to be less ambigious so that it actually describes the data it is receiving so that it can be passed into the correct serializer, which would mean it would need to know the API route the received data came from. Not sure how to do that except with a one class per route, how I currently have it setup.

Then there would need to be a way to specify multiple serializers in a view to send the respective data to. Not seeing that this is possible.

# ./urls.py

from .views import (
    SecurityQuestionsAPIView,
    UserSigninTokenAPIView,
    UsernameRecoveryAPIView,
    ValidateKeyAPIView
)

urlpatterns = [
    url(r'^signin/', UserSigninTokenAPIView.as_view(), name='signin'),
    url(r'^verify/', verify_jwt_token),
    url(r'^refresh/', refresh_jwt_token),
    url(r'^username_recovery/', UsernameRecoveryAPIView.as_view(), name='username_recovery'),
    url(r'^validate_key/', ValidateKeyAPIView.as_view(), name='validate_key'),
    url(r'^security_questions/', SecurityQuestionsAPIView.as_view(), name='security_questions'),
]

# ./views.py
from .serializers import (
    SecurityQuestionsSerializer,
    UserSigninTokenSerializer, 
    UsernameRecoverySerializer,
    ValidateKeySerializer
)

# Used for logging into the web application
class UserSigninTokenAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = UserSigninTokenSerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = UserSigninTokenSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class UsernameRecoveryAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = UsernameRecoverySerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = UsernameRecoverySerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class ValidateKeyAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = ValidateKeySerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = ValidateKeySerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class SecurityQuestionsAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = SecurityQuestionsSerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = SecurityQuestionsSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

Upvotes: 0

Views: 1194

Answers (1)

Linovia
Linovia

Reputation: 20996

You are likely looking for the CreateApiView

class UserSigninTokenAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = UserSigninTokenSerializer


class UsernameRecoveryAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = UsernameRecoverySerializer


class ValidateKeyAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = ValidateKeySerializer


class SecurityQuestionsAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = SecurityQuestionsSerializer

Upvotes: 1

Related Questions