CA Pulkit Sharma
CA Pulkit Sharma

Reputation: 41

Django-excel file imported but models are not saved

Well I am trying to update database by uploading excel file using Django-excel, however the data is not saved to db and neither I get any error message!!!

My views.py

@login_required
def customerbulk(request):    
    action = 1
    if request.method == "POST":
        form = productupload(request.POST, request.FILES)
        def choice_func(row):
            q = request.user
            row[0] = q
            return row
        if form.is_valid():
            request.FILES['select_excel_file'].save_book_to_database(
                models=[customer],
                initializers=[choice_func],
                mapdicts=[

                    ['user','name','address','state_code','shipping_address','shipping_state_code','email','telephone','GSTIN','PAN','discount','notes']]
            )
            return HttpResponseRedirect(reverse('customerbulk'),messages.add_message(request, messages.SUCCESS,'Customers added Succesfully'))       
    else:
        form = productupload()
    return render(request,'productbulk.html',{'form': form,'action':action,})

Upvotes: 0

Views: 388

Answers (1)

CA Pulkit Sharma
CA Pulkit Sharma

Reputation: 41

I think the problem is with package or I missed it but documentation part is unclear. The issue is that if you have a field which has default value but is not null=true and blank=true then you need to supply a value in excel cell.

For example, I had a field

igst_rate=....(default=0)

I solved it by modifying it to:

igst_rate=....(default=0,null=true,blank=true)

Hope it solves problem of anyone else having same issue.

Upvotes: 0

Related Questions