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)
def get(self, request, format=None): queryset = Host.objects.filter(asset_type__isnull=True) ##########以下内容在数据展示列表中不需要修改 pg = MyPageNumberPagination() # 实例化分页类 page_data = pg.paginate_queryset(queryset=queryset, request=request, view=self) # 根据请求的页码数,对数据进行分页 s = Host_Serializer(instance=page_data, many=True) # 序列花这个分页数据 next = pg.get_next_link() # 获取下一页 prev = pg.get_previous_link() # 获取上页 count = queryset.count() # 获取数据总数 json_data = {'code': 0, 'msg': 'success', 'count': count, 'next': next, 'prev': prev} json_data['data'] = s.data return Response(json_data)