Q:

Django customise response for success and fail

I want to create customise response for both success and fail for 1 of my serializer. Right now i only have the create function for success only
I want the output to display like the default output + my 2 other message.
promptmsg and status.
eg output of json data: if success: 
promptmsg = "You have successfully create xxx"
status = '200'  
if fail
promptmsg = "You have fail to create xxx"
status = '400'  
Here is the code for my views
class ScheduleViewSet(viewsets.ModelViewSet):
    permission_classes = [AllowAny]
    queryset = Schedule.objects.all()
    serializer_class = ScheduleSerializer

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if not serializer.is_valid(raise_exception=False):
            return Response({"promptmsg": "You have failed to register an account",
                             "status": "400"}, status=HTTP_400_BAD_REQUEST)

        response = super(ScheduleViewSet, self).create(request, *args, **kwargs)
        response.data['promptmsg'] = 'You have successfully create a book'
        response.data['statuscode'] = '200'
        return response

    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        if not serializer.is_valid(raise_exception=False):
            return Response({"promptmsg": "You have failed to register an account",
                             "statuscode": "400"}, status=HTTP_400_BAD_REQUEST)

        # serializer.is_valid(raise_exception=True)
        # self.perform_update(serializer)
        response = super(ScheduleViewSet, self).update(request, *args, **kwargs)
        response.data['promptmsg'] = 'You have successfully create a book'
        response.data['statuscode'] = '200'
        return response

As you can see, if fail, it will just return the promptmsg and status only.
If success will display default response + promptmsg + status.
So how do i change it ?

A:

If I understood correctly you need to add error details to your custom error response in case of serializing failed? In this case you can use serializer.errors attribute:
if not serializer.is_valid(raise_exception=False):
    errors_details = serializer.errors
    errors_details["promptmsg"] = "You have failed to register an account"
    errors_details["status"] = "400"
    return Response(errors_details, status=HTTP_400_BAD_REQUEST)

