只有备用(偶数行)才会导入到django

时间:2017-07-16 10:21:37

标签: python django excel xlsx pyexcel

我正在使用django上的excel文件上传 使用相同的方法我使用xlsx文件填充了另一个模型。但在第二个模型中我无法上传,我面临的问题我在下面提到过。

  

views.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})
  

models.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 file which I'm tryig to upload

在上图中只上传了sl_no 1,3,5的行

  

结果   enter image description here

1 个答案:

答案 0 :(得分:0)

谢谢你的支持!! @ chfw 代码工作。 实际上问题在于excel文件中的日期格式。 Django接受yyyy-mm-dd,但输入日期格式为dd-mm-yyyy