Django之批量插入数据、 自定义分页器

本文介绍了Django中批量插入数据的方法,包括普通插入和bulk_create高效方法。接着详细讲解了自定义分页器的实现过程,从分页推导、数据总页面获取到利用内置方法divmod优化,最后展示了自定义分页器的封装和使用。
摘要由CSDN通过智能技术生成


一、 批量插入数据

项目需求:浏览器中访问django后端某一条url(如:127.0.0.1:8080/index/),实时朝数据库中生成一千条数据并将生成的数据查询出来,并展示到前端页面

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 Book(models.Model):
  title = models.CharField(max_length=64)

template/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' %}">
    <link rel="stylesheet" href="{% static 'dist/sweetalert.css' %}">
    <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
    <script src="{% static 'dist/sweetalert.min.js' %}"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            {
   % for book in book_queryset %}
            <p>{
   {
    book.title }}</p>
            {
   % endfor %}
        </div>
    </div>
</div>
</body>
</html>

方法一:

views.py

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

def get_book(request):
  # for循环插入1000条数据
  for i in range(1000):
    models.Book.objects
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值