def U_data(request, id_): ''' update specific data using id. ''' data = JSONParser().parse(request) if request.method == 'PUT': if 'data' not in data: return JsonResponse({"message": "{data:[{}]..}"}) if 'tablename' not in data: return JsonResponse({"message": "Body:tablename"}) tablename = data['tablename'].lower() input_ = data['data'][0] model, serializers = main(tablename) if tablename == "domaintestlog": domaintestlog = model.objects.using('default').get(id=id_) # print(domaintestlog.CDN) # domaintestlog.TestTime = data["TestTime"] # domaintestlog.save() data_serializer = serializers(domaintestlog, data=input_) if data_serializer.is_valid(): data_serializer.save() successed = {"successed": data_serializer.data} return JsonResponse(successed, status=status.HTTP_200_OK) return JsonResponse(data_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif tablename == "domainlistall": domainlistall = model.objects.using('default').get(id=id_) data_serializer = serializers(domainlistall, data=input_) if data_serializer.is_valid(): data_serializer.save() successed = {"successed": data_serializer.data} return JsonResponse(successed, status=status.HTTP_200_OK) return JsonResponse(data_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse({"message": "Table doesn't exist."})
def C_data(request): ''' add new data to database.''' data = JSONParser().parse(request) if request.method == 'POST': if 'tablename' not in data: return JsonResponse({"message": "Body:tablename"}) tablename = data['tablename'].lower() input_ = data['data'] model, serializers = main(tablename) if tablename == "domaintestlog": data_serializer = serializers(data=input_, many=True) if data_serializer.is_valid(): data_serializer.save() return JsonResponse(data, safe=False, status=status.HTTP_201_CREATED) return JsonResponse(data_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST) elif tablename == "domainlistall": data_serializer = serializers(data=input_, many=True) if data_serializer.is_valid(): data_serializer.save() return JsonResponse(data, safe=False, status=status.HTTP_201_CREATED) return JsonResponse(data_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse({"message": "Table doesn't exist."})
class GoodListView(View): def get(self, request): """ 通过django的view实现商品列表页 :param request: :return: """ json_list = [] goods = Goods.objects.all()[:10] for good in goods: json_dict = {} json_dict["name"] = good.name json_dict["category"] = good.category.name json_dict["market_price"] = good.market_price json_dict["add_time"] = goos.add_time json_dict.append(json_dict) return HttpResponse(json.dums(json_list), content_type="application/json") # 方法二 from django.forms.models import mode_to_dict for good in goods: json_dict = model_to_dict(good) json_list.append(json_dict) import json from django.core import serializers json_data = serializers("json", goods) json_data = json.loads(json_data)
def R_data(request): '''show data''' if request.method == 'GET': tablename = request.GET.get("tablename", None) filter_data = request.GET.get("filter_data", None) if not tablename: return JsonResponse({'message': 'Params:tablename'}) tablename = tablename.lower() model, serializers = main(tablename) if tablename == "domaintestlog": domaintestlog = model.objects.using('default').all() if filter_data: data = json.loads(filter_data) try: domaintestlog = model.objects.using( 'default').complex_filter(data) except Exception as e: return JsonResponse({'message': f'{e}'}) if domaintestlog.count() == 0: return JsonResponse({'message': 'No data.'}) data_serializer = serializers(domaintestlog, many=True) results = {"results": data_serializer.data} return JsonResponse(results, safe=False) elif tablename == "domainlistall": domainlistall = model.objects.using('default').all() if filter_data: data = json.loads(filter_data) try: domainlistall = model.objects.using( 'default').complex_filter(data) except Exception as e: return JsonResponse({'message': f'{e}'}) if domainlistall.count() == 0: return JsonResponse({'message': 'No data.'}) data_serializer = serializers(domainlistall, many=True) results = {"results": data_serializer.data} return JsonResponse(results, safe=False) else: return JsonResponse({"message": "Table doesn't exist."})
def getData(tablename, filter_data): model, serializers = main(tablename) if not model or not serializers: err_msg = "model or serializers not found." return False, [], err_msg if filter_data: data = json.loads(filter_data) try: queryset = model.objects.using('slave').complex_filter(data) except Exception as e: err_msg = f'{e}' return False, [], err_msg else: queryset = model.objects.using('slave').all() if queryset.count() == 0: err_msg = 'No data' return False, [], err_msg data_serializer = serializers(queryset, many=True) data = data_serializer.data return True, data, ""