Here's the question.
I want to use django's bulk_create
to save more datas at once. But the original result I get from API is a generator with amount data in it. So I want to loop this generator and bulk save data.
My trial was as below:
# a generator with amount data
l = (
item for item in range(1,100230, 1)
)
# base model table
class ModelDemo(models.Model):
...
# main logic code
limit = 2000
while l:
bulk_list = []
for index, item in enumerate(l):
bulk_list.append(
ModelDemo(
...
)
)
if index == limit:
ModelDemo.objects.bulk_create(bulk_list)
break
It's obviously I would lose last 230
data, but I couldn't find the solution by now.
Any commentary is very welcome. great thanks.