为什么每次只能导入一行数据呢?
def import_devices_excel(request): # 1.接收excel文件并存储到media文件夹 #return HttpResponse(json.dumps(request.FILES)) rev_file = request.FILES.get('device') #return JsonResponse({'code': 22, 'msg': 'Excel文件不存在!'+rev_file.name}) # 判断是否有文件 if not rev_file: return JsonResponse({'code': 5, 'msg': 'Excel文件不存在!'}) # 获得一个唯一的名字:uuid +hash new_name = get_random_str() file_path = os.path.join(settings.MEDIA_ROOT, new_name + os.path.splitext(rev_file.name)[1]) #return JsonResponse({'filepath':file_path}) try: f = open(file_path, 'wb') for i in rev_file.chunks(): f.write(i) f.close() except Exception as e:
return JsonResponse({'code': 6})
ex_devices = read_excel_dict(file_path)
success = 0
error = 0
error_snos = []
for one_device in ex_devices:
try:
obj_device = Device.objects.create(name=one_device['name'],
department=one_device['department'],
device_brand=one_device['device_brand'],
device_type=one_device['device_type'],
classified=one_device['classified'],
vlan=one_device['vlan'],
device_model=one_device['device_model'],
device_sn=one_device['device_sn'],
ip_address=one_device['ip_address'],
subnet_mask=one_device['subnet_mask'],
gateway_address=one_device['gateway_address'],
sfp_mac_address=one_device['sfp_mac_address']
, mac_address=one_device['mac_address'],
ssd_sn=one_device['ssd_sn'],
mhd_sn=one_device['mhd_sn'],
ico_sn=one_device['ico_sn'],
dksj_account=one_device['dksj_account']
, exchange_board=one_device['exchange_board'],
switch_port=one_device['switch_port'],
device_status=one_device['device_status'],
red_power=one_device['red_power'],
video_jammer=one_device['video_jammer'],
others_notes=one_device['others_notes'],
red_disk_sn=one_device['red_disk_sn'],
purchase_date=one_device['purchase_date'])
# obj_device.save()
success += 1
except:
error += 1
error_snos.append(one_device['device_sn'])
返回信息
obj_devices = Device.objects.all().values()
devices = list(obj_devices)
return JsonResponse({'code':1 , 'success':success ,'error':error,'errors':error_snos,'data': devices})
评论 0