首页 > 数据库 > Flask 学习-73.Flask-SQLAlchemy 分页查询paginate

Flask 学习-73.Flask-SQLAlchemy 分页查询paginate

时间:2022-09-22 19:37:33浏览次数:54  
标签:SQLAlchemy 分页 Flask items self paginate per query page

前言

Flask-SQLAlchemy 提供了一个分页查询方法 paginate(),方便我们实现在后端查询分页。

分页查询

在django 框架里面有个rest_framework.pagination 分页器, 只需简单的配置就可以实现分页

from rest_framework.pagination import PageNumberPagination

# 定义分页器简单分页(PageNumberPagination)
class MyPageNumberPagination(PageNumberPagination):
    page_size = 50                   # 默认每页显示的多少条记录
    page_query_param = 'page'         # 默认查询参数名为 page
    page_size_query_param = 'size'    # 前台控制每页显示的最大条数
    max_page_size = 100              # 后台控制显示的最大记录条数

Flask-SQLAlchemy 也提供了一个 paginate()查询方法, 相关源码如下

    def paginate(self, page=None, per_page=None, error_out=True, max_per_page=None):
        """Returns ``per_page`` items from page ``page``.

        If ``page`` or ``per_page`` are ``None``, they will be retrieved from
        the request query. If ``max_per_page`` is specified, ``per_page`` will
        be limited to that value. If there is no request or they aren't in the
        query, they default to 1 and 20 respectively.

        When ``error_out`` is ``True`` (default), the following rules will
        cause a 404 response:

        * No items are found and ``page`` is not 1.
        * ``page`` is less than 1, or ``per_page`` is negative.
        * ``page`` or ``per_page`` are not ints.

        When ``error_out`` is ``False``, ``page`` and ``per_page`` default to
        1 and 20 respectively.

        Returns a :class:`Pagination` object.
        """

        return Pagination(self, page, per_page, total, items)

参数说明:
page: 指定页码,从1开始
per_page: 每一页显示几条数据
error_out:是否抛出错误(默认为True)大部分error_outFalse, page and per_page 默认值是 1和20
max_per_page:每页显示最大值当指定了max_per_page时,per_page会受到这个值的限制

调用 paginate()查询方法会返回一个Pagination 对象的实例

class Pagination(object):
    """Internal helper class returned by :meth:`BaseQuery.paginate`.  You
    can also construct it from any other SQLAlchemy query object if you are
    working with other libraries.  Additionally it is possible to pass `None`
    as query object in which case the :meth:`prev` and :meth:`next` will
    no longer work.
    """

    def __init__(self, query, page, per_page, total, items):
        #: the unlimited query object that was used to create this
        #: pagination object.
        self.query = query
        #: the current page number (1 indexed)
        self.page = page
        #: the number of items to be displayed on a page.
        self.per_page = per_page
        #: the total number of items matching the query
        self.total = total
        #: the items for the current page
        self.items = items

Pagination类对象的属性主要有:
has_next:如果在目前页后至少还有一页的话,返回 True。
has_prev:如果在目前页之前至少还有一页的话,返回 True。
next_num:下一页的页面数。
prev_num:前一页的页面数。

另外还有如下的可调用方法:
iter_pages():一个迭代器,返回一个在分页导航中显示的页数列表。
prev():上一页的分页对象。
next():下一页的分页对象。

实例属性有
query:创建Pagination对象对应的query对象
total:匹配的元素总数
per_page:每一页显示的元素个数
items:当前页面的查询结果

分页查询接口

from flask import make_response, request
from http import HTTPStatus

@api.route('/demo')
class ProjectInfoView(Resource):


    @api.doc(description='查询')
    @api.marshal_with(project_model)
    def get(self):
        """查询全部"""
        api.logger.info(f"GET query查询参数: {request.args}")
        # 按id倒序
        objs = Demo.query.order_by(Demo.id.desc())
        # 分页 page=None, per_page=None, error_out=True, max_per_page=None
        page_objs = objs.paginate(
            page=int(request.args.get("page")),
            per_page=int(request.args.get("size")),
            error_out=False,
            max_per_page=50
        ).items
        return page_objs, HTTPStatus.OK

分页接口查询示例/demo?page=1&size=3

标签:SQLAlchemy,分页,Flask,items,self,paginate,per,query,page
From: https://www.cnblogs.com/yoyoketang/p/16720577.html

相关文章

  • Flask 学习-72.Flask-RESTX 自定义输出日期格式
    前言DateTime类型可以支持2种时间格式RFC822和ISO8601,如果需要输出自己想要的格式,需要自定义日期类型。日期格式DateTime类型可以支持2种时间格式RFC822和ISO......
  • Flask 学习-1.简介与环境准备
    前言Flask是由python开发的轻量的web框架,小巧,灵活,一个脚本就可以启动一个web项目,上手非常容易。Flask和Django框架对比Django:大而全,有一套完整的框架,但是耦合性高......
  • Flask 学习-71.Flask-RESTX 枚举类型Enum字段
    前言model中有枚举类型字段使用示例模型设计classTeachers(db.Model):__tablename__='teacher'#数据库表名id=db.Column(db.Integer,primary_key=T......
  • flask 博客系统 逻辑关系
    主干部门提交代码评论的逻辑......
  • Flask 学习-70.Flask-RESTX 注册接口实例
    前言注册接口主要是密码需要加密,用到werkzeug.security模块的2个方法generate_password_hash,check_password_hash.数据库操作用到Flask-SQLAlchemy,相关的基础配置就......
  • 在Linux服务器上部署Flask
    要使用Flask,需要对MVC有一定了解构建PythonWeb项目,首先安装Flask:pip3installflask安装完成后,使用flask--version确保安装成功:然后创建Web目录和app目录设为~/demo......
  • flask升序降序,分页,序列化器
    classUserInfoView(Resource):#@marshal_with(field)defget(self):#daming=User('daming',40,None)#xiaoming=User('xiaoming',16,damin......
  • Flask学习笔记(一)-最小实例+路由
    一、Flask说明Flask是一个使用Python编写的轻量级Web应用框架。其WSGI工具箱采用Werkzeug,模板引擎则使用Jinja2。Flask使用BSD授权。Flask也被称为“microfr......
  • Flask设置logger日志模板
    logger日志模板"""flask设置logger日志统一目录:Myapp/utils/logger.py"""importloggingfromlogging.handlersimportRotatingFileHandlerclassLogger(object):......
  • Flask 学习-55.文件上传功能开发
    前言文件上传的基本原理实际上很简单,基本上是:一个带有enctype=multipart/form-data的标记,标记中含有一个。应用通过request对象的files字典来访问文件。使......