0

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
klementine
  • 300
  • 5
  • 12
  • Does this answer your question? [memory-efficient built-in SqlAlchemy iterator/generator?](https://stackoverflow.com/questions/7389759/memory-efficient-built-in-sqlalchemy-iterator-generator) – ljmc Jan 13 '23 at 22:38

1 Answers1

0

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
klementine
  • 300
  • 5
  • 12