drf 自定义response


'''
Response({
'status':0,
'msg':'ok',
'results':[],
'额外返回一些':''
},headers={},status=200,content_type='')

APIResponse(0,'ok',results,status,header,content_type)
'''

from rest_framework.response import Response

class APIResponse(Response):
    def __init__(self,data_status,data_msg,results=None,
                 status=None,headers=None,content_type=None,**kwargs):
        data = {
            'status':data_status,
            'msg':data_msg
        }
        if results is not None:
            data['results'] = results

        data.update(**kwargs)

        super().__init__(data=data,status=status,headers=headers,content_type=content_type)

猜你喜欢

转载自www.cnblogs.com/Afrafre/p/11865659.html
DRF