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
  • 441,113
  • 110
  • 1,021
  • 1,148
David542
  • 101,766
  • 154
  • 423
  • 727
  • 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
  • 441,113
  • 110
  • 1,021
  • 1,148