我需要将7,000天的小时温度数据(所以7 * 24 * 11,000条数据记录)的数据加载到我的模型11,000个站点。我需要一天做两次。使用自动提交非常慢。我想在从每个网站加载数据后提交。我有两个模型班,Lake和Tempdat。代码的本质在下面。我感谢您提供的任何帮助。 (!谢谢),我一直在寻找Django文档和其他例子在本网站的原子,但我需要更简单的指令Django 1.10从CSV文件记录中手动提交数据库
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)`
Django不提交每条记录。 –
所以用命令:c.tempdat_set.create(LakeTemp = temperature,ModelDate = dateob),数据没有提交?我的理解是,对于过去的版本,您可以在保存或创建多个记录后进行提交。如果情况并非如此,有没有办法提高这个过程的速度 – user3472574
它似乎并不像一个Django的问题。这更像你的算法。请发布您的模型和广告几个示例行 – e4c5