[Answer]-Insert CSV data into django model using executemany

1๐Ÿ‘

You could do something like this.

#models.py
class JNJUsage(models.Model):
    ...

# views.py (where ever def upload is)
to_create = []

for i, row in enumerate(reader):
    j = JNJUsage()
    j.country = row['country']
    j.no_of_people_house = row['no_of_people_house']
    j.nursing_cnt = row['nursing_cnt']
    j.city = row['city']
    j.sec = row['sec']
    j.bucket = row['bucket']
    j.category1 = row['category1']
    j.category2 = row['category2']
    j.final_category = row['final_category']
    j.responders = row['responders']
    j.usageFrequency = row['usageFrequency']
    j.base = row['base']
    j.child_age_group = row['child_age_group']

    to_create.append(j)

    # If 900k is too much then you could consider something like this
    if i % 10000 == 0:
        JNJUsage.objects.bulk_create(to_create)
        to_create = []

# Clean up the rest
JNJUsage.objects.bulk_create(to_create)
๐Ÿ‘คNathaniel

0๐Ÿ‘

Of course, you can use bulk_create. It would look something like this:

def upload(request):
    if request.method == 'POST':
        cursor = connection.cursor()
        x=[]
        reader = csv.reader(request.FILES['csvData'],delimiter=',')
        i = 0
        for row in reader:
            obj = new MyObject()
            obj.country = row[0]
            obj.city = row[3]
            ...
            x.append(obj)
            if i>=5000:
                MyObject.objects.bulk_create(x)
                x=[]
                i=0

            i = i+1
        return HttpResponse( docfile.name + "'s data inserted into database successfully")

You can find more info about bulk_create in docs.

0๐Ÿ‘

I got it. Just checked the size of the row and then escaped that particular row, and also i was getting some characters like โ€œ\x00โ€, so used regular expression to remove them.

def upload(request):
    start_time = time.time()
    print start_time
    if request.method == 'POST':
        cursor = connection.cursor()    
        x=[]
        docfile = request.FILES['csvData']

        reader = csv.reader(request.FILES['csvData'],delimiter=',')

        to_create = []
        for i, row in enumerate(reader):
            if len(row) != 13:
                reader.next()
                continue

            j = JnJUsage()
            j.country =row[0]
            j.no_of_people_house = row[1]
            j.nursing_cnt = row[2]
            j.city = row[3]
            j.sec = row[4]
            j.bucket = row[5]
            j.category1 = row[6]
            j.category2 = row[7]
            j.final_category = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]', '', row[8])
            j.responders = row[9]
            j.usageFrequency = row[10]
            j.base = row[11]
            j.child_age_group = row[12]

            to_create.append(j)

            # If 900k is too much then you could consider something like this
            if i % 10000 == 0:
                JnJUsage.objects.bulk_create(to_create)
                to_create = []
        JnJUsage.objects.bulk_create(to_create)

        return HttpResponse( docfile.name + "'s data inserted into database successfully")

Thanks Dmitry Mikhaylov and Nathaniel for helping out

๐Ÿ‘คvaibhav1312

Leave a comment