可以使用Django Rest Framework为每个视图设置多个序列化程序

时间:2017-10-04 16:15:34

标签: django django-rest-framework

在我工作的时候,我的./views.py变得非常有竞争力,我想知道是否有办法将其重构为干。我认为为了做到这一点,我需要能够为每个视图使用指定多个序列化器。

data需要不那么暧昧,以便它实际描述它正在接收的数据,以便它可以传递到正确的序列化器,这意味着它需要知道接收数据来自的API路由从。不知道如何做到这一点,除了每个路线一个班级,我目前如何设置。

然后需要一种方法在视图中指定多个序列化程序以发送相应的数据。没有看到这是可能的。

# ./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)

1 个答案:

答案 0 :(得分:1)

您可能正在寻找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