[Django]-Object of type '' is not JSON serializable in DRF


You need to return serializer.data instead of serializer.validated_data.

Have a look at the updated code for ReservationViewSet:

class ReservationViewSet(CreateModelMixin, GenericViewSet):
    serializer_class = ReservationSerializer
    queryset = Reservation.objects.all()

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        return Response(status=status.HTTP_201_CREATED, data=serializer.data)

the data property is meant to translate the model instance into the Python native dictionary type. Python Dictionary can be serialized as a JSON Response.

Leave a comment