我正在使用相同的方法在 django 中上传 excel 文件,我使用 xlsx 文件填充了另一个模型。但是在第二个模型中,我无法上传,并且遇到了我在下面提到的问题。
视图.py
def import_source(request):
if request.method == "POST":
form = UploadFileForm(request.POST,
request.FILES)
if form.is_valid():
request.FILES['file'].save_to_database(
name_columns_by_row=0,
model=Tracker,
mapdict=['sl_no','dot','branch','sname','snum','can_name','sdetail','main_skills','uname','cus_name','ep_num','day','doi','status','cont_num','email_id','texp','rexp','cu_org','cu_loc','pref_loc','cu_ctc','exp_ctc','notice_period'])
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
'upload_form.html',
{'form': form})
模型.py
class Tracker(models.Model):
sl_no = models.IntegerField()
dot = models.DateTimeField()
branch = models.CharField(max_length=200)
sname = models.CharField(max_length=200)
snum = models.CharField(max_length=200)
can_name = models.CharField(max_length=200)
sdetail = models.CharField(max_length=200)
main_skills = models.TextField()
uname = models.CharField(max_length=200)
cus_name = models.CharField(max_length=200)
ep_num = models.CharField(max_length=200,primary_key=True)
day = models.CharField(max_length=200)
doi = models.DateTimeField()
status = models.CharField(max_length=200)
cont_num = models.IntegerField()
email_id = models.EmailField(max_length=200)
texp = models.DecimalField(max_digits=4,decimal_places=2)
rexp = models.DecimalField(max_digits=4,decimal_places=2)
cu_org = models.CharField(max_length=200)
cu_loc = models.CharField(max_length=200)
pref_loc = models.CharField(max_length=200)
cu_ctc = models.DecimalField(max_digits=4,decimal_places=2)
exp_ctc = models.DecimalField(max_digits=4,decimal_places=2)
notice_period = models.IntegerField()
我正在尝试上传的Excel 文件 .xlsx 文件
在上图中,仅上传了 sl_no 为 1、3、5 的行
结果 在此处输入图像描述