我在 Django 中上传了一个 4000 行的 excel(我使用 django-excel 作为外部插件),这大约需要 17 秒。与预计约为 600-700 的普通 POSTGRESQL 写入 qps(每秒查询数)相比,这听起来非常低。为了保持数据完整性并添加特定列,我需要一次输入一行。以下是我目前正在使用的代码。
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})
学生验证功能如下:
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 中导入大约 4K 行的 excel)?