Book Image

Web Development with Django - Second Edition

By : Ben Shaw, Saurabh Badhwar, Chris Guest, Bharath Chandra K S
4.7 (3)
Book Image

Web Development with Django - Second Edition

4.7 (3)
By: Ben Shaw, Saurabh Badhwar, Chris Guest, Bharath Chandra K S

Overview of this book

Do you want to develop reliable and secure applications that stand out from the crowd without spending hours on boilerplate code? You’ve made the right choice trusting the Django framework, and this book will tell you why. Often referred to as a “batteries included” web development framework, Django comes with all the core features needed to build a standalone application. Web Development with Django will take you through all the essential concepts and help you explore its power to build real-world applications using Python. Throughout the book, you’ll get the grips with the major features of Django by building a website called Bookr – a repository for book reviews. This end-to-end case study is split into a series of bitesize projects presented as exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. As you advance, you'll acquire various practical skills, including how to serve static files to add CSS, JavaScript, and images to your application, how to implement forms to accept user input, and how to manage sessions to ensure a reliable user experience. You’ll cover everyday tasks that are part of the development cycle of a real-world web application. By the end of this Django book, you'll have the skills and confidence to creatively develop and deploy your own projects.
Table of Contents (19 chapters)

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='[email protected]'), Publisher(name="Byron
        Bay Press", website="www.byronbayexample.com",
        email='[email protected]'),
        Publisher(name="Katoomba Publisher",
        website="www.katoombaexample.com",
        email='[email protected]')])
    [<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.