How to batch sqlalchemy results by size

Question:

I have a model Post and want to iterate through them in batches of 10 in a loop.

This is what I’ve tried, but does not work:

batched_posts = Post.query.yield_for(10)
for posts in batched_posts.partitions(): # error: 'Query' object has no attribute 'partitions'
  print(len(posts)) # prints 10 ten times if I have 100 posts
Asked By: klementine

||

Answers:

Ended up using .paginate instead

paginated_posts = Post.query.paginate(per_page=10)

while paginated_posts.items:
  print(len(paginated_posts.items)) # paginated_posts.items will be a list
Answered By: klementine
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.