Flask-flask中的后台分页查询实现

在后台查询数据并在前台展示的场景中,当数据量较大时,页面加载会非常缓慢,此时建议使用后台分页查询的形式。在flask中,基于Flask-SQLAlchemy可以使用以下方式实现。

方法一:

Flask-SQLAlchemy 提供了一个 paginate()查询方法,参考:Flask 学习-73.Flask-SQLAlchemy 分页查询paginate_flasksqlalchemy分页查询_上海-悠悠的博客-CSDN博客

复制代码
page4 = Server.query.paginate(page=4, per_page=2)
print(page4.items)
page3 = page4.prev()
print(page3.items)

查看源码:

复制代码
    def paginate(
        self,
        select: sa.sql.Select[t.Any],
        *,
        page: int | None = None,
        per_page: int | None = None,
        max_per_page: int | None = None,
        error_out: bool = True,
        count: bool = True,
    ) -> Pagination:
        """Apply an offset and limit to a select statment based on the current page and
        number of items per page, returning a :class:`.Pagination` object.

        The statement should select a model class, like ``select(User)``. This applies
        ``unique()`` and ``scalars()`` modifiers to the result, so compound selects will
        not return the expected results.

        :param select: The ``select`` statement to paginate.
        :param page: The current page, used to calculate the offset. Defaults to the
            ``page`` query arg during a request, or 1 otherwise.
        :param per_page: The maximum number of items on a page, used to calculate the
            offset and limit. Defaults to the ``per_page`` query arg during a request,
            or 20 otherwise.
        :param max_per_page: The maximum allowed value for ``per_page``, to limit a
            user-provided value. Use ``None`` for no limit. Defaults to 100.
        :param error_out: Abort with a ``404 Not Found`` error if no items are returned
            and ``page`` is not 1, or if ``page`` or ``per_page`` is less than 1, or if
            either are not ints.
        :param count: Calculate the total number of values by issuing an extra count
            query. For very complex queries this may be inaccurate or slow, so it can be
            disabled and set manually if necessary.

        .. versionchanged:: 3.0
            The ``count`` query is more efficient.

        .. versionadded:: 3.0
        """

参数说明:

page: int,指定页码,从1开始

per_page: int,每一页显示几条数据

max_per_page:每页显示最大值当指定了max_per_page时,per_page会受到这个值的限制

error_out:是否抛出错误(默认为True)

count:是否计数,默认为True

返回值:

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

Pagination类对象的属性主要有:

has_next:如果在目前页后至少还有一页的话,返回 True。

has_prev:如果在目前页之前至少还有一页的话,返回 True。

next_num:下一页的页面数。

prev_num:前一页的页面数。

另外还有如下的可调用方法:

iter_pages():一个迭代器,返回一个在分页导航中显示的页数列表。

prev():上一页的分页对象。

next():下一页的分页对象。

方法二:

利用限制查询方式手动实现分页,参考:Flask 查询,分页,排序及逻辑运算与聚合:_flask分页查询_Gray area的博客-CSDN博客

复制代码
filters=[] # 组装通用查询过滤器filter,略
offset = (int(paging_start)-1)*int(paging_size)   
data_qry = User.query.filter(and_(*filters)).offset(offset).limit(paging_size).all()
或者
data_qry = User.query.offset(offset).limit(paging_size).all()
相关推荐
专业程序开发源2 分钟前
springboot外卖系统94294-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计
计算机毕业编程指导师2 分钟前
【Python毕设选题推荐】基于Spark的国内主要农作物产量趋势分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·python·数据分析·spark·毕业设计·课程设计·农作物产量
计算机毕业编程指导师8 分钟前
【Python毕设选题推荐】基于Spark的宫颈癌变光谱特征数据分析可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·spark·毕业设计·宫颈癌变
ss2738 分钟前
AI全栈实战 | 3.3-01 Python OOP:__init__ 真的是构造函数吗,元类怎么让 Django Model 变魔法
开发语言·python·django
FYKJ_201011 分钟前
springboot心理健康管理系统86254-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·mysql·django·课程设计
海绵宝宝转agent9 小时前
learn-claude-code第1-5章开源学习笔记分享
人工智能·笔记·python·学习
小奇不哭9 小时前
ROS2 单节点,RGB-D 深度相机局部路径提取 + B 样条平滑路径 + YOLOv8 障碍物检测 + 深度测距,用于移动机器人视觉局部循迹。
python·yolov8·ros2·cv2·路径探索循迹
小小龙学IT9 小时前
Python scikit-learn 机器学习库深度解析
python·机器学习·scikit-learn
2601_9620715710 小时前
类变量和全局变量的查找路径有什么区别?
开发语言·python
卷无止境11 小时前
独立开发者的"富矿地带":哪些垂直领域值得你押注一辈子?
后端·python