1

I have a list of tuples, where every tuple element belongs to a particular column, for example:

[("mike",19,"graduate"),("niko",22,"software engineer")....] 

model/table:
Name | Age | occupation

how do I insert every tuple into a table (I have the required model created already). Just don't know how to insert into the database.

Please help and forgive me if I come off as an amateur. I am a beginner.

nar-007
  • 119
  • 2
  • 9

1 Answers1

2

You can make a list of objects, and then .bulk_create(..) [Django-doc] these:

data = [
    MyModel(name=name, age=age, occupation=occupation)
    for name, age, occupation in mylistoftuples
]

MyModel.objects.bulk_create(data)
Willem Van Onsem
  • 397,926
  • 29
  • 362
  • 485