Bulk update using one query over Django ORM.
Likely abandoned. No release or commit for years (last release over 9 years ago).
pip install django-bulk-updatedjango-bulk-update
Simple bulk update over Django ORM or with helper function.
This project aims to bulk update given objects using one query over Django ORM.
Installation
pip install django-bulk-update
Usage
With manager:
import random
from django_bulk_update.manager import BulkUpdateManager
from tests.models import Person
class Person(models.Model):
...
objects = BulkUpdateManager()
random_names = ['Walter', 'The Dude', 'Donny', 'Jesus']
people = Person.objects.all()
for person in people:
person.name = random.choice(random_names)
Person.objects.bulk_update(people, update_fields=['name']) # updates only name column
Person.objects.bulk_update(people, exclude_fields=['username']) # updates all columns except username
Person.objects.bulk_update(people) # updates all columns
Person.objects.bulk_update(people, batch_size=50000) # updates all columns by 50000 sized chunks
With helper:
import random
from django_bulk_update.helper import bulk_update
from tests.models import Person
random_names = ['Walter', 'The Dude', 'Donny', 'Jesus']
people = Person.objects.all()
for person in people:
person.name = random.choice(random_names)
bulk_update(people, update_fields=['name']) # updates only name column
bulk_update(people, exclude_fields=['username']) # updates all columns except username
bulk_update(people, using='someotherdb') # updates all columns using the given db
bulk_update(people) # updates all columns using the default db
bulk_update(people, batch_size=50000) # updates all columns by 50000 sized chunks using the default db
Note: You can consider to use .only('name') when you only want to update name, so that Django will only retrieve name data from db.
And consider to use .defer('username') when you don't want to update username, so Django won't retrieve username from db.
These optimization can improve the performance even more.
Performance Tests:
Here we test the performance of the bulk_update function vs. simply calling
.save() on every object update (). The interesting metric is the speedup using
the function more than the actual raw times.