Doing bulk update and bulk create with Django ORM
It's not unusual the need to do bulk update/create in django applications, but if you don't use the right approach your views will increase the load time to unacceptable values.
Here is the common example where people starts out:# took 37 seconds def auto_transaction():
for i in range(10000):
name="String number %s"%i
Record.objects.create(name=name)
Before django 1.4 we didn't have the built-in bulk_create, so the common way to do a bulk insertion was disabling the auto transaction in the beginning of operation and do it only once in the end:# took 2.65 seconds
@transaction.commit_manually
def manual_transaction():
for i in range(10000):
name="String number %s"%i
Record.objects.create(name=name)
transaction.commit()
But since Django 1.4 an