def post(self, request, format=None): file_obj = request.data["file"] data = pd.read_excel(file_obj) try: for index, row in data.iterrows(): goods = Goods() goods.gtin = row['id'] goods.company = row['company'] goods.spec = row['spec'] goods.name = row['name'] goods.brand = row['brand'] if len(eval(row['classes'])) > 0: goods.is_class = 1 goods.save() classes = eval(row['classes']) for class_name in classes: category = GoodsCategory.objects.filter( name=class_name) goods.classes.add(category[0]) else: goods.save() except Argument: print(Argument) return Response({"msg": "上传文件失败,请检查文件格式是否正确"}) else: return Response(status=201, data={"code": 201, "msg": "上传数据成功"}) # print(data) # print("here") # return Response(status=201) # def DashboardView(self): # total = Goods.objects.all().count() # is_class = Goods.objects.filter(is_class=1).count() # no_class = total - is_class # class_per = is_class / total # json_data = { # "total": total, # "is_class": is_class, # "no_class": no_class, # "percent": class_per # } # return JsonResponse(json_data,safe=False) # class DashboardView(View): # def get(self, request): # total = Goods.objects.all().count() # is_class = Goods.objects.filter(is_class=1).count() # no_class = total - is_class # class_per = is_class / total # json_data = { # "total": total, # "is_class": is_class, # "no_class": no_class, # "percent": class_per # } # return JsonResponse(json.dump(json_data),safe=False)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ScProduct.settings') import django django.setup() from goods.models import Goods, GoodsCategory data = pd.read_csv('data/water_classnum.csv') # from db_tools.data.product_data import row_data for index, row in data.iterrows(): # if index > 5: # break # print(len(eval(row['classes']))) goods = Goods() goods.gtin = row['id'] goods.company = row['company'] goods.spec = row['spec'] goods.name = row['name'] goods.brand = row['brand'] if len(eval(row['classes'])) > 0: goods.is_class = 1 goods.save() classes = eval(row['classes']) for class_name in classes: category = GoodsCategory.objects.filter(name=class_name) goods.classes.add(category[0]) else: goods.save() print(index)