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


    批量插入数据

    【1】准备数据

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

    【2】一条一条插入

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

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

    【3】优化-批量插入

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

    分页的原理及推导

    1. 当查询的数据太多的时候,一页展示不完,分页码展示
    2. """
    3. 总数据 每页展示 总页数
    4. 100 10 10
    5. 101 10 11
    6. 99 10 10
    7. 怎么计算出来总页数
    8. 总数据 / 每页展示 = 总页数
    9. 有余数+1
    10. 没有余数=商
    11. """
    12. divmod
    分页类
    1. class Pagination(object):
    2. def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):
    3. """
    4. 封装分页相关数据
    5. :param current_page: 当前页
    6. :param all_count: 数据库中的数据总条数
    7. :param per_page_num: 每页显示的数据条数
    8. :param pager_count: 最多显示的页码个数
    9. """
    10. try:
    11. current_page = int(current_page)
    12. except Exception as e:
    13. current_page = 1
    14. if current_page < 1:
    15. current_page = 1
    16. self.current_page = current_page
    17. self.all_count = all_count
    18. self.per_page_num = per_page_num
    19. # 总页码
    20. all_pager, tmp = divmod(all_count, per_page_num)
    21. if tmp:
    22. all_pager += 1
    23. self.all_pager = all_pager
    24. self.pager_count = pager_count
    25. self.pager_count_half = int((pager_count - 1) / 2)
    26. @property
    27. def start(self):
    28. return (self.current_page - 1) * self.per_page_num
    29. @property
    30. def end(self):
    31. return self.current_page * self.per_page_num
    32. @property
    33. def page_html(self):
    34. # 如果总页码 < 11个:
    35. if self.all_pager <= self.pager_count:
    36. pager_start = 1
    37. pager_end = self.all_pager + 1
    38. # 总页码 > 11
    39. else:
    40. # 当前页如果<=页面上最多显示11/2个页码
    41. if self.current_page <= self.pager_count_half:
    42. pager_start = 1
    43. pager_end = self.pager_count + 1
    44. # 当前页大于5
    45. else:
    46. # 页码翻到最后
    47. if (self.current_page + self.pager_count_half) > self.all_pager:
    48. pager_end = self.all_pager + 1
    49. pager_start = self.all_pager - self.pager_count + 1
    50. else:
    51. pager_start = self.current_page - self.pager_count_half
    52. pager_end = self.current_page + self.pager_count_half + 1
    53. page_html_list = []
    54. # 添加前面的nav和ul标签
    55. page_html_list.append('''
      • ''')
      • first_page = '
      • 首页
      • ' % (1)
    56. page_html_list.append(first_page)
    57. if self.current_page <= 1:
    58. prev_page = '
    59. 上一页
    60. '
  • else:
  • prev_page = '
  • 上一页
  • ' % (self.current_page - 1,)
  • page_html_list.append(prev_page)
  • for i in range(pager_start, pager_end):
  • if i == self.current_page:
  • temp = '
  • %s
  • ' % (i, i,)
  • else:
  • temp = '
  • %s
  • ' % (i, i,)
  • page_html_list.append(temp)
  • if self.current_page >= self.all_pager:
  • next_page = '
  • 下一页
  • '
  • else:
  • next_page = '
  • 下一页
  • ' % (self.current_page + 1,)
  • page_html_list.append(next_page)
  • last_page = '
  • 尾页
  • ' % (self.all_pager,)
  • page_html_list.append(last_page)
  • # 尾部添加标签
  • page_html_list.append('''
  • ''')
  • return ''.join(page_html_list)
  • 相关阅读:
    以MixtralForCausalLM为例,演示如何不依赖框架实现pipeline并行
    Go基础3:函数、结构体、方法、接口
    python基础练习题目
    PyTorch
    每天学习一点英语——number,amount,quantity区别、用法
    【qstock】几行代码实现数据获取、可视化到量化选股实战
    压力测试-Locust框架基本使用及更新报错解决方案
    【附源码】计算机毕业设计JAVA基于Web的社区商超系统的设计与实现
    PromptPort:为大模型定制的创意AI提示词工具库
    硬件【11】超全讲解I2C的上拉电阻
  • 原文地址:https://blog.csdn.net/m0_71292438/article/details/134536530