10

I have this code and the all() method and every other method works on this and I have looked all over and I could that the method paginate() works on BaseQuery which is also Query

@app.route('/')
@app.route('/index')
@app.route('/blog')
@app.route('/index/<int:page>')
def index(page = 1):
 posts = db.session.query(models.Post).paginate(page, RESULTS_PER_PAGE, False)
return render_template('index.html', title="Home", posts=posts)

but this gives me the error AttributeError: 'Query' object has no attribute 'paginate' I've looked everywhere and I can't find any solution to this.

asked Aug 27, 2013 at 14:54

1 Answer 1

27

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(...)
answered Aug 27, 2013 at 17:10
Sign up to request clarification or add additional context in comments.

Thanks this worked! I was indeed getting confused between the two. This cleared it up for me :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.