1

Given the following data, how would I do a BulkInsert in django?

list_of_data = [
    {'name': 'Paul'}, 
    {'name': 'Robert'
]

# with a normal insert
for data in list_of_data:
    Person.objects.create(name=data['name'])
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    I don't think it is difficult to take a look at the [documentation](https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create) for bulk insert. – Ozgur Vatansever Feb 08 '15 at 02:02

1 Answers1

3

Use bulk_create():

This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are)

Person.objects.bulk_create([Person(**data) for data in list_of_data])
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195