Flask SQLAlchemy pagination error

核能气质少年 提交于 2019-11-30 20:12:52

From your question...

that the method paginate() works on BaseQuery which is also Query

I think this is where you're being confused. "Query" refers to the SQLAlchemy Query object. "BaseQuery" refers to the Flask-SQLALchemy BaseQuery object, which is a subclass of Query. This subclass includes helpers such as first_or_404() and paginate(). However, this means that a Query object does NOT have the paginate() function. How you actually build the object you are calling your "Query" object depends on whether you are dealing with a Query or BaseQuery object.

In this code, you are getting the SQLAlchemy Query object, which results in an error:

db.session.query(models.Post).paginate(...)

If you use the following code, you get the pagination you're looking for, because you are dealing with a BaseQuery object (from Flask-SQLAlchemy) rather than a Query object (from SQLAlchemy).

models.Post.query.paginate(...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!