from rest_framework.response import Response from rest_framework.status import is_success class ResponseFormat: def __init__(self,msg,data,code): self.msg = msg self.data = data self.code = code def response(self): # if self.code == 201: # return Response({'msg':self.msg,'data':self.data,'code':self.code}) print(self.data) if is_success(self.code): return Response({'msg':self.msg,'data':self.data,'code':self.code}) return Response({'msg':'here is some error','data':self.data,'code':self.code}) class UnifiedResponseAPIView: def create(self, request, *args, **kwargs): response = super().create(request, *args, **kwargs) res = ResponseFormat( msg='success', data=response.data, code=response.status_code ) return res.response() def list(self, request, *args, **kwargs): response = super().list(request, *args, **kwargs) res = ResponseFormat( msg='success', data=response.data, code=response.status_code ) return res.response() def retrieve(self, request, *args, **kwargs): response = super().retrieve( request, *args, **kwargs) res = ResponseFormat( msg='success', data=response.data, code=response.status_code ) return res.response() def update(self, request, *args, **kwargs): # no request.data response = super().update( request, *args, **kwargs) res = ResponseFormat( msg='success', data=response.data, code=response.status_code ) return res.response() def destroy(self, request, *args, **kwargs): response = super().destroy( request, *args, **kwargs) res = ResponseFormat( msg='success', data=response.data, code=response.status_code ) return res.response()