From what I understand there are two things happening:
- Pagination of object Posts
- Saving a new information on each row that is retrieved
Part 1: Pagination is a class returned by paginate
and the way to iterate through the elements is via items
. That is why you are getting a TypeError
when trying to iterate through the Pagination
class. You can see more on the docs, or on this answer
The correct way to iterate would be:
for post in posts.items:
# your code
Part 2: If you want to update the object as you iterate though it, you would also need to add it and commit. I am not sure the object can actually be updated as you are looping, but you could try:
session = db.session
for post in posts.items:
post.views += 1
session.add(post)
session.commit()
return render_template('view.html',posts=posts)
If it does not work you can either append the ids to a list and update all at the end, or query and update inside the loop, using a different session.
posts_rendered = []
for post in posts.items:
posts_rendered.append(post.id)
session.query(Posts).filter(Posts.id.in_(posts_rendered))\
.update({Posts.views: Posts.views + 1})
session.commit()
A great response on updates can be found here