Andrejovic Andrej
Andrejovic Andrej

Reputation: 218

CSV to Database in Django

I'm receiving data from upload form on my site, and I would like to insert the data to a Database. In views.py I'm using this code

def upload(request):
    data = {}
    if "GET" == request.method:
        return render(request, "main/upload.html", data)
    # if not GET, then proceed

    csv_file = request.FILES["csv_file"]
    if not csv_file.name.endswith('.csv'):
        messages.error(request,'File is not CSV type')
        return HttpResponseRedirect(reverse("main:upload"))
        #if file is too large, return
    if csv_file.multiple_chunks():
        messages.error(request,"Uploaded file is too big (%.2f MB)." % (csv_file.size/(1000*1000),))
        return HttpResponseRedirect(reverse("main:upload"))

    file_data = csv_file.read().decode("utf-8")
    lines = file_data.split("\n")
        #io_string = io.StringIO(data_set)
        #print(io_string.getvalue())
        #next(io_string)
    for line in lines:
        column = line.split(",")
        data_dict = {}
        data_dict["顧客CD"]=column[0],
        data_dict["顧客補助CD"]=column[1],
        data_dict["顧客名称1"]=column[2],
        data_dict["顧客名称"]=column[3],
        data_dict["顧客名称カナ"]=column[4],
        data_dict["法人名称"]=column[5],
        data_dict["代表者名称"]=column[6],
        data_dict["住所"]=column[7],
        data_dict["電話番号"]=column[8],
        data_dict["地区名称"]=column[9],
        data_dict["データマッチ用電話番号"]=column[10],
        data_dict["契約状態"]=column[11]
        form = kokyaku(data_dict)
        form.save()

    return HttpResponseRedirect(reverse("main:upload"))

And in models.py this

from djongo import models
from django.utils import timezone
from django.contrib.auth.models import User

class kokyaku(models.Model):
    顧客CD = models.IntegerField(blank=True)
    顧客補助CD = models.IntegerField(blank=True)
    顧客名称1 = models.TextField(blank=True)
    顧客名称 = models.TextField(blank=True)
    顧客名称カナ = models.TextField(blank=True)
    法人名称 = models.CharField(max_length=15, blank=True)
    代表者名称 = models.CharField(max_length=15, blank=True)
    住所 = models.TextField(blank=True)
    電話番号 = models.IntegerField(blank=True)
    地区名称 = models.TextField(blank=True)
    データマッチ用電話番号 = models.IntegerField(blank=True)
    契約状態 = models.CharField(max_length=2, blank=True)

    def __str__(self):
        return self.顧客名称

As you can see, I'm trying to "match" the columns from views to models with python's dict, but in models I'm trying to get it as variables, so my program raises and err msg: int() argument must be a string, a bytes-like object or a number, not 'dict' But I'M not sure how could I change the format to be able to pass the information as dict to models or change the format to make models be able to handle this

Upvotes: 0

Views: 483

Answers (1)

BATMAN
BATMAN

Reputation: 373

You can simply try to typecast them, and instead of saving form for each instance you can just add instances to the database like this. (Even the for will work, just make sure that you typecast them properly and there is no weird strings going in int() )

for line in lines:
        column = line.split(",")
        kokyaku.objects.create(顧客CD = column[0], 顧客補助CD = int(column[1]),顧客名称1=str(column[2]), 顧客名称=str(column[3]), 顧客名称カナ=str(column[4]), 法人名称=str(column[5]), 代表者名称=str(column[6]),住所=str(column[7]), 電話番号=str(int(column[8])),地区名称=str(column[9]),データマッチ用電話番号=int(column[10]),契約状態=str(column[11]))

To Import the data directly, you might want to take a look at tablib library. It has useful and efficient tools. In django specifically you can make use of django-import-export they work well with tablib.

Upvotes: 2

Related Questions