def post(self, request, format=None): s = Host_Serializer(data=request.data) if s.is_valid(): # 验证 s.save() json_data = {'code': 200, 'msg': '数据添加成功'} json_data['data'] = s.data return Response(json_data, content_type="application/json") # return api_response.JsonResponse(s.data,code=status.HTTP_200_OK,msg='success') json_data = {'code': 500, 'msg': '数据添加失败,请检查数据格式'} return Response(json_data, content_type="application/json")
def put(self, request, format=None): try: data_id = request.data['id'] except Exception as e: print(e) json_data = {'code': 500, 'msg': '数据有错误获取不到id'} return Response(json_data,status=500) else: DATA_MODEL = self.get_object(data_id) s = Host_Serializer(DATA_MODEL, data=request.data) if s.is_valid(raise_exception=True): s.save() json_data = {'code': 200, 'msg': '更新成功'} return Response(json_data) json_data = {'code': 500, 'msg': '更新失败'} return Response(json_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)