I'm wanting to convert an API from flask to Django Rest Framework. Currently, the requirements are that the responses are put inside of a basic json structure.
eg.
{
"status": "success",
"data": {actual results here}
}
What's the best way to do this?
A generic approach would be to inherit from the DRF's JSONRenderer and adapt it to your needs, see http://www.django-rest-framework.org/api-guide/renderers/.
Aditionally you propably want to adapt the pagination style to your old flask api, see http://www.django-rest-framework.org/api-guide/pagination/.
You can create a custom response:
from rest_framework.response import Response
class CustomSuccessResponse(Response):
def __init__(self, data=None):
result = {
'status': 'success',
'data': data,
}
super(CustomSuccessResponse, self).__init__(data=result)
And then in your view you can use it like this:
return CustomSuccessResponse(data={'message': 'actual results'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With