Django rest_framework custom error message

前端 未结 3 701
梦谈多话
梦谈多话 2021-01-15 15:27

I have a API endpoint where it will do input validation using rest_framework\'s serializer.is_valid() where it will return custom error message and response.

3条回答
  •  孤城傲影
    2021-01-15 15:53

    You can write custom error handler:

    from rest_framework.views import exception_handler
    
    def custom_exception_handler(exc, context):
        response = exception_handler(exc, context)
        if response is not None:
            response.data['Failure'] = 'Error'
    
        return response
    

提交回复
热议问题