Implementation of batch data insertion in Django

Time:2021-2-25

Project requirements: access a URL of Django back end in browser (for example: 127.0.0.1:8080 / get)_ Generate 1000 pieces of data in the database in real time, query the generated data and display it to the front page

views.py

from django.shortcuts import render, HttpResponse, redirect
from app01 import models

def get_book(request):
  #For loop inserts 1000 data
  for i in range(1000):
    models.Book.objects . create (name ='Book% s% I)
  book_ queryset =  models.Book.objects . all () # query the inserted data again
  return render(request,'get_ book.html , locales ()) ා transfer the query data to the HTML page

urls.py


from django.conf.urls import url
from app01 import views

urlpatterns = [
 url(r'^get_book/',views.get_book)
]

models.py


from django.db import models

class get_book(models.Model):
 title = models.CharField(max_length=64)

get_book.html


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  {% load static %}
  <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}" rel="external nofollow" >
  <link rel="stylesheet" href="{% static 'dist/sweetalert.css' %}" rel="external nofollow" >
  <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
  <script src="{% static 'dist/sweetalert.min.js' %}"></script>
</head>
<body>
{% for book_obj in book_queryset %}
   <p>{{ book_obj.title }}</p>
{% endfor %}
</body>
</html>

After the above code is written, start the Django back-end and visit the browser. You will find that the browser will have an obvious stuck waiting time. This is because the back-end is constantly operating the database, which takes a long time. It will take a long time to see the 1000 pieces of data just inserted. Obviously, the efficiency of operating the database is too low. Is there a way Is it specially used for batch operation of database? The answer is yes!

bulk_ Create method

take views.py The original view function is slightly changed

def get_book(request):
  l = []
  for i in range(10000):
    l.append( models.Book (title ='Book% s% I))
  models.Book.objects .bulk_ Create (L) # batch insert data
    return render(request,'get_book.html',locals())

After the code is modified, there is no need to change other places. Restart the Django project browser and re visit it. You will immediately find that when the amount of data increases ten times, the page appears faster than above.

bulk_ The Create method is specially provided by Django ORM for us to operate the database in batches, which is very efficient!!!

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]