Writing your own reponse function will not help, in case you are using class based views say APIView
or generic views. I think a better way could be to write custom renderer for formatting successfull responses and custom exception handler for responses with errors. This implementation gives you a single point to control all reponses without changing your already existing views. Here is the sample code from one of my project. You need modify it as per your requirements.
Custom Renderer
#renderers.py
class CustomJSONRenderer(JSONRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
response_data = {'message': '', 'errors': [], 'data': data, 'status': 'success'}
getattr(renderer_context.get('view').get_serializer().Meta,'resource_name', 'objects')
# call super to render the response
response = super(CustomJSONRenderer, self).render(response_data, accepted_media_type, renderer_context)
return response
Custom Exception Handler
# custom_exception_handler.py
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
errors = []
message = response.data.get('detail')
if not message:
for field, value in response.data.items():
errors.append("{} : {}".format(field, " ".join(value)))
response.data = {'data': [], 'message': 'Validation Error', 'errors': errors, 'status': 'failure'}
else:
response.data = {'data': [], 'message': message, 'error': [message], 'success': 'failure'}
return response
Override REST_FRAMEWORK
settings to use these renderer and exception handler.
REST_FRAMEWORK = {
# Add custom renderer.
'DEFAULT_RENDERER_CLASSES'=(
'path.to.CustomJSONRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer'),
# Add cutom exception handler
'EXCEPTION_HANDLER': 'path.to.custom_exception_handler'
}
Serializers
, django-rest-framework.org/api-guide/serializers – Aila