DRF10大接口
自定义Respoense 虽然我们已经在使用,rest_framework的Response,但是代码还是有点冗余,于是就可以自己重写一下(具体封装可以更具实际需求变更) 重写 from rest_framework.response import Response class APIResponse(Response): # 格式化data def __init__(self, status=0, msg='ok', results=None, http_status=None, headers=None, exception=False, **kwargs): # data默认加上json的response基础有数据状态码和数据状态信息 data = { 'status': status, 'msg': msg } # 如果后台有数据,响应数据 if results is not None: data['results'] = results # 后台的一切自定义响应数据直接放到响应数据data中 data.update(**kwargs) super().__init__(data=data, status=http_status, headers=headers, exception=exception) 使用 #查询正确,修改默认的状态码,和msg和http状态码