0

As the title indicates, is there a way to bulk_create list of lists. Like right now I bulk_create it like -

for i in range(len(x))   
  arr1 = []
    for m in range(len(y)):
    arr1.append(DataModel(foundation=foundation, date=dates[m], price=price[m]))
  DataModel.objects.bulk_create(arr1)

Now this will bulk create till the length of x.

Can it be done like so -

arr = []
for i in range(len(x))   
  arr1 = []
    for m in range(len(y)):
    arr1.append(DataModel(foundation=foundation, date=dates[m], price=price[m]))
  arr.append(arr1)
DataModel.objects.bulk_create(arr)

If not, what else can be done to store data faster?

Sushant
  • 3,499
  • 3
  • 17
  • 34

2 Answers2

0

Append your object to arr, not arr1.
Or you can make flat list before bulk_create:

import itertools
arr = list(itertools.chain(*arr))
DeKaNszn
  • 2,720
  • 3
  • 26
  • 26
0

Try this....

arr = []
for i in range(len(x))   
  arr1 = []
    for m in range(len(y)):
    arr1.append(DataModel(foundation=foundation, date=dates[m], price=price[m]))
  #instead of appending the list, add list together to make one
  arr = arr + arr1
DataModel.objects.bulk_create(arr)

this will produce a single list of items process-able by bulk_create() method

reference: Python append() vs. + operator on lists, why do these give different results?

Community
  • 1
  • 1
Nouman Tahir
  • 819
  • 1
  • 9
  • 26