Bird
0
0

You want to add pagination to your repository method fetching all posts. Which of the following correctly implements pagination with page number and page size parameters?

hard📝 Application Q9 of 15
Flask - Ecosystem and Patterns
You want to add pagination to your repository method fetching all posts. Which of the following correctly implements pagination with page number and page size parameters?
Adef get_posts(self, page, size): return Post.query.limit(size).offset(page).all()
Bdef get_posts(self, page, size): return Post.query.paginate(page=page, per_page=size).items
Cdef get_posts(self, page, size): return Post.query.paginate(page, size).all()
Ddef get_posts(self, page, size): return Post.query.paginate(page=page, per_page=size).all()
Step-by-Step Solution
Solution:
  1. Step 1: Recall Flask-SQLAlchemy paginate method signature

    It uses named parameters: page and per_page.
  2. Step 2: Understand return type

    paginate() returns a Pagination object; .items gets current page records.
  3. Step 3: Match correct option

    def get_posts(self, page, size): return Post.query.paginate(page=page, per_page=size).items correctly uses named parameters and accesses .items.
  4. Final Answer:

    def get_posts(self, page, size): return Post.query.paginate(page=page, per_page=size).items -> Option B
  5. Quick Check:

    Use paginate(page=, per_page=).items for pagination [OK]
Quick Trick: Use paginate(page=, per_page=).items for paged queries [OK]
Common Mistakes:
MISTAKES
  • Using offset with page number directly
  • Calling all() on paginate result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes