[Python] How to parse api request to defined model?

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new with python, I'm trying to parse api request to a defined model, so how to fix below code?

    class PersonSerializer(serializers.Serializer):
    name = serializers.CharField()

    class APIJustTest(APIView):
    parser_classes = (JSONParser,)
    @swagger_auto_schema(request_body=PersonSerializer)
    def post(self, request, *args, **krgs):
    serializer = PersonSerializer(data=request.data)
    serializer.is_valid()

    nameOK = serializer['name'].value #dictionary works but I prefer defined model
    nameError = serializer.name #this is defined model but will cause server error

    return JsonResponse({})

    Continue reading...

Compartilhe esta Página