Django 批量插入数据到数据库

# 首先进入项目虚拟环境
$ pipenv shell

# 进入 Django shell
$ python manage.py shell

import pandas as pd

from apps.audience import models

# chunksize 每次读取的数据量
data = pd.read_csv("xxx.csv",chunksize=10000)

for items in data:
    insert_list = []
    for item in items.values:
        insert_list.append(models.BaseTagAudienceSample(id=item[0], ids_id=item[1], tags_id=item[2]))
    # 批量插入
    models.BaseTagAudienceSample.objects.bulk_create(insert_list)

你可能感兴趣的:(Django,django)