Bulk create and bulk update operations
When we have a large set of records that needs to be created or updated, we can perform bulk create and bulk update operations using the bulk_create()
and bulk_update()
methods, respectively. When we have a large number of records to be created or updated, using methods such as these can be efficient when performing database operations.
Typically, here is how a bulk_create
method is called by supplying a list of objects:
Person.objects.bulk_create([ Person(name='Robert', address='5, Byron bay, NSW'), Person(name='Mark', address="Unit 12, New town, NSW 2000"),])
Likewise, the bulk update is also performed on a list of similar objects. The object’s attributes can be changed as shown in the following example and updated in the database in a single command using the bulk_update()
method, as shown in the following snippet:
persons[0].address = "8, Byron bay, NSW" persons[1].address = "15, New town, NSW" Person.objects.bulk_update(persons, ["address"])
We will see further examples of both of these in the following exercises.
Exercise 2.15 – creating multiple records using bulk_create
In this exercise, we will use the bulk_create()
method to create multiple entries into the Publisher
table in one go. To do that, follow these steps:
- Import the
Publisher
model if you have not already imported it into your Django shell:>>> from reviews.models import Publisher
- Create multiple records in the
Publisher
table by passing a list of thePublisher
objects into thebulk_create()
method:>>> Publisher.objects.bulk_create([ Publisher(name="New Town Publisher", website="www.newtownexample.com", email='newtow@email.com'), Publisher(name="Byron Bay Press", website="www.byronbayexample.com", email='byronbayexample@email.com'), Publisher(name="Katoomba Publisher", website="www.katoombaexample.com", email='katoombaexample@email.com')]) [<Publisher: New Town Publisher>, <Publisher: Byron Bay Press>, <Publisher: Katoomba Publisher>]
The method returns a list of objects created as entries in the database. If a large number of entries need to be created, this might be one of the most efficient ways of carrying out the task.
Next, we shall try to bulk update records efficiently using the bulk_update
method.
Exercise 2.16 – updating multiple records using bulk_update
Similar to the previous exercise, we can update multiple records in one go by using the bulk_update
method:
- First, import the
Publisher
model if you have not already imported it:>>> from reviews.models import Publisher
- In the next step, we will pick a couple of
Publisher
objects as a list. - Assuming both of these publishers were merged into a single company, and they need to share the same website, now let us update both the objects in a query set:
>>> publishers = [Publisher.objects.get(name='New Town Publisher'), Publisher.objects.get(name='Byron Bay Press')] >>> publishers[0].website = "www.newsouthwalespublisher.com" >>> publishers[1].website = "www.newsouthwalespublisher.com" >>> Publisher.objects.bulk_update(publishers, ["website"]) 2
Upon running the bulk_update
method, it returns the number of objects updated; in this case, it is two objects. Again, this is an efficient way to update more than one object in one go.