Given a Django REST Framework view using PageNumberPagination with page_size=3, what will the JSON response contain when requesting page 2?
from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response class MyPagination(PageNumberPagination): page_size = 3 # Assume queryset has 7 items indexed 1 to 7 # Request: GET /items/?page=2 # What will be the 'results' length in the response?
PageNumberPagination divides items into pages of fixed size. Page 1 has items 1-3, page 2 has items 4-6, and so on.
With 7 items and page size 3, page 1 has items 1-3, page 2 has items 4-6, and page 3 has item 7. So page 2 returns 3 items.
Choose the correct way to define a cursor pagination class with a page size of 5 and ordering by 'created' field.
The ordering attribute expects an iterable of strings.
The ordering attribute must be a list or tuple of strings. Option D uses a list with one string 'created'.
Examine the following Django REST Framework pagination class and identify why it raises a TypeError when used.
from rest_framework.pagination import LimitOffsetPagination class MyLimitOffsetPagination(LimitOffsetPagination): default_limit = '10' # Using this pagination causes TypeError: unsupported operand type(s) for -: 'str' and 'int'
Check the type of default_limit and how it is used internally.
LimitOffsetPagination expects default_limit as an integer. Setting it as a string causes arithmetic operations to fail.
Given a queryset of 12 items and PageNumberPagination with page_size=5, what will the 'next' URL be when requesting page 2?
# Request: GET /api/items/?page=2 # Page size: 5 # Total items: 12 # What is the 'next' URL in the paginated response?
Calculate total pages and which page comes after page 2.
With 12 items and page size 5, total pages = 3 (5+5+2). Page 2's next page is page 3, so 'next' URL points to page 3.
You want to implement infinite scrolling in a Django REST Framework API. The data changes frequently, and you want to avoid skipping or repeating items when users scroll. Which pagination method is best?
Consider how data changes affect pagination stability and user experience.
CursorPagination uses a cursor based on item ordering, preventing duplicates or skips even if data changes. It is ideal for infinite scrolling.