How to use bulk_update() in Django?

Published On: 15/09/2022 | Category: Django

Hi Dev,

In this post, we will learn how to use bulk_update() in django app. if you want to see example of django bulk_update() example then you are a right place. This tutorial will give you simple example of django bulk update function example. I would like to share with you django bulk update or create example.

Django bulk_update() is a method that updates the given fields on the provided model instances with one query.

Here i explained simply step by step example of how to use bulk_update() in django app.


bulk_update(objs, fields, batch_size=None)

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names and store bulk of data in database. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core

Step 3: Update

In this step we require to do add installed apps in our file. Add the below lines to your file:

Next, you need to add it in the file as follows:




Step 4: Create a Model

In this step now go for the models we will We'll call our single model Post and it will have just two fields: title and content. And finally set __str__ to display the name of the post.

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=250)
    content = models.TextField()

    def __str__(self):
        return self.title

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python makemigrations
python migrate

Step 5: Update File

In this step the Post class of the models is registered using the register() method to display the books tables in the Django administration dashboard.

 Import admin module

from django.contrib import admin

# Import Post model

from .models import Post

# Register Post model

Step 6: Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the core/ file and add:

from django.shortcuts import render
from django.views.generic import ListView
from .models import Post

# Create your views here.
class BulkUpdate(ListView):

    model = Post
    template_name = 'postlist.html'
    queryset = Post.objects.all()

    if queryset.exists() == True:

        update_queries = []

        a = Post.objects.get(id=1)
        b = Post.objects.get(id=2)
        c = Post.objects.get(id=3)

        #set update value
        a.title="Hello django updated"
        b.title="Hello python updated"
        c.title="Hello php updated"

        update_queries.extend((a, b, c))
        Post.objects.bulk_update(update_queries, ['title'])

    def get_queryset(self):
        return Post.objects.all()

Step 7: Creating the Templates

Next, then with your text editor create new templates files: core/templates/postlist.html file and the add:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]6.1/dist/css/bootstrap.min.css">
    <style type="text/css">
            background-color: #f7fcff;
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5>How to use bulk_update() in Django App? - <span class="text-primary"></span></h5>
                    <div class="card-body">
                        <table class="table table-bordered">
                                {% for post in object_list %}
                                        <td>{{ post.title }}</td>
                                        <td>{{ post.content }}</td>
                                {% endfor %}

Step 8: Creating URLs

In this section, we’ll create the urls to access our CRUD views.Go to the core/ file and update it as follows:

from django.urls import path
from core import views

urlpatterns = [
    path('post/', views.BulkUpdate.as_view()),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/post/ address with a web browser.

I Hope It will help you....

Happy Pythonic Coding!!