在Django上传一个相对较大的excel并提高写入速度(Postgresql)

我在Django上传了一个4000行的excel(我使用django-excel作为外部插件),大约需要17秒。 这听起来非常低,相比普通的POSTGRESQL写qps(查询每秒)预计是约600-700.为了保持数据的完整性,并添加一个特定的列,我需要做一次input一行。 以下是我目前正在使用的代码。

def import_student(request): this_tenant=request.user.tenant if request.method == "POST": form = UploadFileForm(request.POST, request.FILES) batch_selected=Batch.objects.for_tenant(this_tenant).get(id=1) def choice_func(row): choice_func.counter+=1 data=student_validate(row, this_tenant, choice_func.counter, batch_selected) return data choice_func.counter=0 if form.is_valid(): with transaction.atomic(): try: request.FILES['file'].save_to_database( model=Student, initializer=choice_func, mapdict=['first_name', 'last_name', 'dob','gender','blood_group', 'contact', 'email_id', \ 'local_id','address_line_1','address_line_2','state','pincode','batch','key', 'slug', 'tenant','user']) return redirect('student:student_list') except: transaction.rollback() return HttpResponse("Failed") else: return HttpResponseBadRequest() else: form = UploadFileForm() return render(request,'upload_form.html',{'form': form}) 

学生validationfunction如下:

 def student_validate(row, this_tenant, counter, batch): data="st" today=dt.date.today() today_string=today.strftime('%y%m%d') next_student_number='{0:03d}'.format(counter) last_student=Student.objects.filter(tenant=this_tenant).\ filter(key__contains=today_string).order_by('key').last() if last_student: last_student_number=int(last_student.key[8:]) next_student_number='{0:03d}'.format(last_student_number + counter) key=data+str(today_string)+str(next_student_number) toslug=str(this_tenant)+" " +str(key) slug=slugify(toslug) item=None row.append(key) row.append(slug) row.append(this_tenant) row[12]=batch if (row[0] == None or row[0] == "" or row[1] == None or row[1] == "") : transaction.rollback() return HttpResponse("There is error in uploaded excel") if (row[3] != "M" and row[3] != "F" and row[3] != "O"): transaction.rollback() return HttpResponse("There is error in uploaded excel") if (row [2] != None and row [2] != ""): if (type(row [2]) != dt.date): transaction.rollback() return HttpResponse("There is error in uploaded excel") return row 

现在有没有办法提高写入速度(在excel中input一个大约4K行的excel)可以改进?