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

批量插入数据

【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 %}
    

{{ book_obj.title }}

{% 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 = 1

        if current_page < 1:
            current_page = 1

        self.current_page = current_page

        self.all_count = all_count
        self.per_page_num = per_page_num

        # 总页码
        all_pager, tmp = divmod(all_count, per_page_num)
        if tmp:
            all_pager += 1
        self.all_pager = all_pager

        self.pager_count = pager_count
        self.pager_count_half = int((pager_count - 1) / 2)

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_num

    @property
    def end(self):
        return self.current_page * self.per_page_num

    @property
    def page_html(self):
        # 如果总页码 < 11个:
        if self.all_pager <= self.pager_count:
            pager_start = 1
            pager_end = self.all_pager + 1
        # 总页码  > 11
        else:
            # 当前页如果<=页面上最多显示11/2个页码
            if self.current_page <= self.pager_count_half:
                pager_start = 1
                pager_end = self.pager_count + 1

            # 当前页大于5
            else:
                # 页码翻到最后
                if (self.current_page + self.pager_count_half) > self.all_pager:
                    pager_end = self.all_pager + 1
                    pager_start = self.all_pager - self.pager_count + 1
                else:
                    pager_start = self.current_page - self.pager_count_half
                    pager_end = self.current_page + self.pager_count_half + 1

        page_html_list = []
        # 添加前面的nav和ul标签
        page_html_list.append('''
                    
                                           
                                       ''')
        return ''.join(page_html_list)

你可能感兴趣的:(python)