Search code examples
androidjsondjangomobiledjango-rest-framework

Common JSON Format for Django Rest Framework responses for Android client


I am developing the rest apis for an android app using Djnago Rest Framework. The android guys told me that it would be nice if I could provide the responses in a common format, like

{
   “Status”:””,
   “Message”:””,
   “Data”:[]
}

Guys, is it the right approach? If so, how to achieve this? My assumption is that using a middleware would be an option.

Thanks in advance.


Solution

  • 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'
       }