建设网站遇到问题的解决方案.耐思尼克官方网站
news/
2025/10/6 16:12:09/
文章来源:
建设网站遇到问题的解决方案,.耐思尼克官方网站,服装品牌策划公司,江苏建新建设集团有限公司网站在后台查询数据并在前台展示的场景中#xff0c;当数据量较大时#xff0c;页面加载会非常缓慢#xff0c;此时建议使用后台分页查询的形式。在flask中#xff0c;基于Flask-SQLAlchemy可以使用以下方式实现。
方法一#xff1a;
Flask-SQLAlchemy 提供了一个 paginate(…在后台查询数据并在前台展示的场景中当数据量较大时页面加载会非常缓慢此时建议使用后台分页查询的形式。在flask中基于Flask-SQLAlchemy可以使用以下方式实现。
方法一
Flask-SQLAlchemy 提供了一个 paginate()查询方法参考Flask 学习-73.Flask-SQLAlchemy 分页查询paginate_flasksqlalchemy分页查询_上海-悠悠的博客-CSDN博客
page4 Server.query.paginate(page4, per_page2)
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 andnumber of items per page, returning a :class:.Pagination object.The statement should select a model class, like select(User). This appliesunique() and scalars() modifiers to the result, so compound selects willnot return the expected results.:param select: The select statement to paginate.:param page: The current page, used to calculate the offset. Defaults to thepage query arg during a request, or 1 otherwise.:param per_page: The maximum number of items on a page, used to calculate theoffset 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 auser-provided value. Use None for no limit. Defaults to 100.:param error_out: Abort with a 404 Not Found error if no items are returnedand page is not 1, or if page or per_page is less than 1, or ifeither are not ints.:param count: Calculate the total number of values by issuing an extra countquery. For very complex queries this may be inaccurate or slow, so it can bedisabled and set manually if necessary... versionchanged:: 3.0The 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()
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/929490.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!