By default, the DRF API interface only returns the JSON string of data, and does not contain other interface information. Sometimes the format is not consistent, which is not very friendly to the front end, so I hope to modify the return information to add some additional information, such as status, description and so on
First, you need to know where to modify the return. Look through the source code and see that the return information is written in the create(), list(), Retrieve (), update(), destroy() methods, all of which are put in the Response object and returned to the front page.
{‘code’: 1, ‘MSG ‘:’ success ‘, ‘errors’: {}, ‘data’: []} # errors puts specific error information, data puts returned data
Then replace the information in Response directly with the dict, as follows:
Replacement, try to request the interface, we define the default return format is like, but didn’t finish task, in the case of only one interface, so change nothing, but if there is more than a dozen, dozens of interface, this interface to change one by one, it’s difficult to maintain, not only tiring so here change my ways.
By defining a class that inherits from ModelViewSet and then inheriting from CustomViewSet instead of ModelViewSet, all interfaces don’t have to change from interface to interface and are easy to maintain.
So far, the task is still not complete, the return of our success is defined, but the interface when an exception occurs, the information returned or default of the information, because we change just normal processing is completed successfully returns, abnormal return we are not processing, where the exception information return should change?
After some debugging, I found that when an exception occurred on the interface, it was handled by the exception_handler method. This method is defined in rest_framework/views.py. Since this method is independent and not defined in ModelViewSet, there is no way to override the method. Let’s copy the whole method. Put it under CustomViewSet (there is no rule to put it together, just put it anywhere for easy management).
As you can see, lines 95 to 98 are where we need to change. Debug to see what the object looks like, and then extract the information we need from it.
The most important step! To add one more configuration to the REST_FRAMEWORK (which you added yourself) in settings.py:
REST_FRAMEWORK = {······ ···'EXCEPTION_HANDLER': 'common.views.exception_handler'
}
Copy the code
This specifies which method to use as the handler for the interface exception, and this is set to the one we just changed.
PS: After making the above changes, we have changed most of the returned information on the system, but sometimes we need to specify an interface that does not inherit from ModelViewSet, so that these changes cannot be inherited. In such cases, we have to manually type the information every time we write the interface, which will be inevitable to type the wrong information by hand. Here I define a class:
Then the interface above can be changed to look like this, more convenient maintenance:
Conclusion:
- Custom one inherits from
ModelViewSet
Class, rewritecreate,list,update,retrieve,destroyThese methods, modify Response - copy
rest_framework/views.py/exception_handler
Method to modify the code that handles exception information inside - in
settings.py
theREST_FRAMEWORK
Add to configuration'EXCEPTION_HANDLER': 'common.views.exception_handler'
- (Optional) Create a return message class
ResturnMsg
, instead of manually writing back information, avoid errors.