当前位置: 首页 > news >正文

批量插入数据与分页的原理及推导

批量插入数据

【1】准备数据

class Book(models.Model):title = models.CharField(max_length=32)

【2】一条一条插入

  • 后端
def ab_many(request):# (1)先给Book表插入一万条数据for i in range(1000):models.Book.objects.create(title=f'第{i}本书')# (2)将所有数据查询到并展示给前端页面book_queryset = models.Book.objects.all()return render(request, 'ab_many.html', locals())
  • 前端
{% for book_obj in book_queryset %}<p>{{ book_obj.title }}</p>
{% endfor %}

效果就是,有一种网络很高的感觉,页面一直在转圈圈

【3】优化-批量插入

def ab_many(request):# 批量插入boo_list = []for i in range(1000):book_obj = models.Book.objects.create(title=f'第{i}本书')boo_list.append(book_obj)models.Book.objects.bulk_create(boo_list)return render(request, 'ab_many.html', locals())
  • 当我们想向数据库批量插入数据的时候,使用ORM提供的bulk_create方法能够大大的减少操作的时间

分页的原理及推导

当查询的数据太多的时候,一页展示不完,分页码展示"""
总数据 		每页展示		总页数
100				10				10
101				10				11
99				10				10
怎么计算出来总页数
总数据  /  每页展示  =  总页数有余数+1
没有余数=商
"""
divmod
分页类
class Pagination(object):def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):"""封装分页相关数据:param current_page: 当前页:param all_count:    数据库中的数据总条数:param per_page_num: 每页显示的数据条数:param pager_count:  最多显示的页码个数"""try:current_page = int(current_page)except Exception as e:current_page = 1if current_page < 1:current_page = 1self.current_page = current_pageself.all_count = all_countself.per_page_num = per_page_num# 总页码all_pager, tmp = divmod(all_count, per_page_num)if tmp:all_pager += 1self.all_pager = all_pagerself.pager_count = pager_countself.pager_count_half = int((pager_count - 1) / 2)@propertydef start(self):return (self.current_page - 1) * self.per_page_num@propertydef end(self):return self.current_page * self.per_page_num@propertydef page_html(self):# 如果总页码 < 11个:if self.all_pager <= self.pager_count:pager_start = 1pager_end = self.all_pager + 1# 总页码  > 11else:# 当前页如果<=页面上最多显示11/2个页码if self.current_page <= self.pager_count_half:pager_start = 1pager_end = self.pager_count + 1# 当前页大于5else:# 页码翻到最后if (self.current_page + self.pager_count_half) > self.all_pager:pager_end = self.all_pager + 1pager_start = self.all_pager - self.pager_count + 1else:pager_start = self.current_page - self.pager_count_halfpager_end = self.current_page + self.pager_count_half + 1page_html_list = []# 添加前面的nav和ul标签page_html_list.append('''<nav aria-label='Page navigation>'<ul class='pagination'>''')first_page = '<li><a href="?page=%s">首页</a></li>' % (1)page_html_list.append(first_page)if self.current_page <= 1:prev_page = '<li class="disabled"><a href="#">上一页</a></li>'else:prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,)page_html_list.append(prev_page)for i in range(pager_start, pager_end):if i == self.current_page:temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,)else:temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,)page_html_list.append(temp)if self.current_page >= self.all_pager:next_page = '<li class="disabled"><a href="#">下一页</a></li>'else:next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,)page_html_list.append(next_page)last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)page_html_list.append(last_page)# 尾部添加标签page_html_list.append('''</nav></ul>''')return ''.join(page_html_list)

http://www.lryc.cn/news/238165.html

相关文章:

  • SMART PLC累计流量功能块(梯形积分法+浮点数累加精度控制)
  • 【金融分析】Python:病人预约安排政策 | 金融模拟分析
  • 后端接口测试,令牌校验住,获取tocken 接口的方式
  • Ghidra逆向工具配置 MacOS 的启动台显示(Python)
  • 关于交换芯片调试 tx_delay rx_delay 的一点经验
  • 【图像分类】【深度学习】【轻量级网络】【Pytorch版本】MobileNets_V2模型算法详解
  • Spring Cloud 简介
  • Redis从入门到精通(二)- 入门篇
  • SpringDoc基础配置和集成OAuth2登录认证教程
  • 链路聚合-静态和动态区别
  • 发币成功,记录一下~
  • 一个完备的手游地形实现方案
  • vite vue3配置axios
  • 使用 C 语言快速排序将字符串按照 ASCII 码升序排列
  • 自动化运维中间件架构概况
  • fractional Brownian Motion driven stochastic integrals
  • c++模式之单例模式详解
  • 【gpts】学算法题[缺失的第一个正数](https://leetcode.cn/problems/first-missing-positive/)
  • 车牌识别 支持12种中文车牌类型 车牌数据集下载
  • Servlet---上传文件
  • 量子计算+物流!“最后一英里”配送难题Unisys成功实时决策
  • 2023年【四川省安全员A证】复审考试及四川省安全员A证考试试题
  • C++刷题 -- 二分查找
  • PHPmail 发送邮件错误 550 的原因是什么?
  • 数字化转型导师坚鹏:数字化时代银行网点厅堂营销5大难点分析
  • www.testfire.nets渗透测试报告
  • 多模态大一统:通向全模态学习和通用人工智能的未来之路
  • 实用篇-ES-DSL查询文档
  • Nacos配置管理
  • 【前端学java】Java中的异常处理(15)完结