Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with Django

You're reading from   Web Development with Django A definitive guide to building modern Python web applications using Django 4

Arrow left icon
Product type Paperback
Published in May 2023
Publisher Packt
ISBN-13 9781803230603
Length 764 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Bharath Chandra K S Bharath Chandra K S
Author Profile Icon Bharath Chandra K S
Bharath Chandra K S
Saurabh Badhwar Saurabh Badhwar
Author Profile Icon Saurabh Badhwar
Saurabh Badhwar
Ben Shaw Ben Shaw
Author Profile Icon Ben Shaw
Ben Shaw
Chris Guest Chris Guest
Author Profile Icon Chris Guest
Chris Guest
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Chapter 1: An Introduction to Django 2. Chapter 2: Models and Migrations FREE CHAPTER 3. Chapter 3: URL Mapping, Views, and Templates 4. Chapter 4: An Introduction to Django Admin 5. Chapter 5: Serving Static Files 6. Chapter 6: Forms 7. Chapter 7: Advanced Form Validation and Model Forms 8. Chapter 8: Media Serving and File Uploads 9. Chapter 9: Sessions and Authentication 10. Chapter 10: Advanced Django Admin and Customizations 11. Chapter 11: Advanced Templating and Class-Based Views 12. Chapter 12: Building a REST API 13. Chapter 13: Generating CSV, PDF, and Other Binary Files 14. Chapter 14: Testing Your Django Applications 15. Chapter 15: Django Third-Party Libraries 16. Chapter 16: Using a Frontend JavaScript Library with Django 17. Index 18. Other Books You May Enjoy

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:

  1. Import the Publisher model if you have not already imported it into your Django shell:
    >>> from reviews.models import Publisher
  2. Create multiple records in the Publisher table by passing a list of the Publisher objects into the bulk_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:

  1. First, import the Publisher model if you have not already imported it:
    >>> from reviews.models import Publisher
  2. In the next step, we will pick a couple of Publisher objects as a list.
  3. 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.

You have been reading a chapter from
Web Development with Django - Second Edition
Published in: May 2023
Publisher: Packt
ISBN-13: 9781803230603
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime