Django 1.10从CSV文件记录手动提交数据库

时间:2017-05-29 19:44:55

标签: python django csv commit atomic

我需要加载到我的模型11,000个站点,每小时温度数据为7天(因此7 * 24 * 11,000个数据记录)的数据。我需要每天做两次。使用自动提交非常慢。我想在从每个站点加载数据后提交。我有两个模型类,Lake和Tempdat。代码的本质如下。我感谢您提供的任何帮助。我一直在关注Django文档中的原子以及本网站上的其他示例,但我需要更直接的指导(谢谢!)

with open('iowa.csv', 'r+') as lakefile:
    lakes = csv.reader(lakefile)
    for lake in lakes:
        gnis = lake[0]

        temp_dat_filename = "T"+str(gnis)+".txt"
        nameanddir=os.path.join(tempdatdir,str(temp_dat_filename))
        f = open(nameanddir, 'r+')
        c = Lake.objects.get(GNIS=gnis) #Lake is the Class name
        print(c)

        for line in f:
            list_of_line = line.rstrip().split()
            date_pieces = list_of_line[0].split('-')

            dateob = datetime.datetime(int(date_pieces[0]), int(date_pieces[1]), int(date_pieces[2]), int(list_of_line[1]), 0, 0)
            temperature = list_of_line[2]
            #NOTE: instead of commiting here for each record, I would to go through all the records (7days worth, then commit)
            c.tempdat_set.create(LakeTemp = temperature, ModelDate = dateob)`

1 个答案:

答案 0 :(得分:0)

你可以使用bulk_create,应该有所帮助 - > https://docs.djangoproject.com/en/1.11/ref/models/querysets/#bulk-create