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
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
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
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