MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Pagination Techniques0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
5.1Filtering API results5.2Search & Query Parameters5.3Pagination Techniques
HomeTutorialsREST API Development with DjangoPagination Techniques
One-to-One, One-to-Many, Many-to-Many

Pagination Techniques

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

DRF Pagination: PageNumber, LimitOffset, and Cursor

Returning a flat JSON array of 500,000 database rows in a single HTTP response crashes client browsers, exhausts server memory, and degrades network latency. Pagination segments large QuerySets into manageable chunks. Django REST Framework provides three distinct pagination schemes: PageNumberPagination, LimitOffsetPagination, and CursorPagination.


1. Comparing DRF's Three Pagination Styles

Pagination StyleURL SignatureDatabase PerformanceStable against Insertions?Client Experience
PageNumberPagination?page=3&page_size=20Moderate (OFFSET)No (duplicate/skipped items)Classic page numbers (1, 2, 3)
LimitOffsetPagination?limit=25&offset=50Moderate (OFFSET)No (page drift on insert)Flexible custom slice loading
CursorPagination?cursor=cD0yMDI2LTA...Optimal (O(1) B-tree)Yes (100% resilient)Infinite scroll, live feeds

2. PageNumberPagination (Classic Page Numbers)

1# api/pagination.py
2from rest_framework.pagination import PageNumberPagination
3from rest_framework.response import Response
4 
5class StandardResultsSetPagination(PageNumberPagination):
6 page_size = 20
7 page_size_query_param = "page_size" # Allows client to override page size
8 max_page_size = 100 # Protects against DoS via ?page_size=100000
9 page_query_param = "page"

Response Envelope:

1{
2 "count": 1420,
3 "next": "https://api.enterprise.com/v1/articles/?page=3",
4 "previous": "https://api.enterprise.com/v1/articles/?page=1",
5 "results": [
6 { "id": 41, "title": "Understanding WebSockets" }
7 ]
8}

3. LimitOffsetPagination (Flexible Range Slicing)

Maps directly to SQL LIMIT and OFFSET:

1from rest_framework.pagination import LimitOffsetPagination
2 
3class CustomLimitOffsetPagination(LimitOffsetPagination):
4 default_limit = 25
5 limit_query_param = "limit"
6 offset_query_param = "offset"
7 max_limit = 100

Client Query:

1GET /api/v1/articles/?limit=10&offset=40 # Fetches items 41 through 50

4. CursorPagination (Infinite Scroll & High Scale)

Both PageNumber and LimitOffset rely on SQL OFFSET. As OFFSET increases into millions (OFFSET 1000000), the database must scan and discard 1,000,000 rows, crippling performance. Furthermore, if a new item is inserted while a user browses page 1, items shift, causing duplicate entries on page 2.

CursorPagination uses an opaque encoded pointer referencing a specific row (e.g. timestamp or ID):

  • Uses SQL WHERE created_at < cursor LIMIT 20 (Instant B-Tree lookup!).
  • 100% immune to missing or duplicate items during real-time writes.
1from rest_framework.pagination import CursorPagination
2 
3class FeedCursorPagination(CursorPagination):
4 page_size = 25
5 cursor_query_param = "cursor"
6 ordering = "-created_at" # Ordering MUST be deterministic!

Response:

1{
2 "next": "https://api.enterprise.com/v1/feed/?cursor=cD0yMDI2LTA5LTE1VDA0...",
3 "previous": null,
4 "results": [...]
5}

Notice: count is omitted because calculating SELECT COUNT(*) on massive tables is expensive!


5. Attaching Pagination to ViewSets

1class ArticleViewSet(ModelViewSet):
2 queryset = Article.objects.all()
3 serializer_class = ArticleSerializer
4 pagination_class = StandardResultsSetPagination

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why does SQL OFFSET become slow on deep pagination (e.g. ?page=10000)?
Question 2
What is the primary advantage of CursorPagination over PageNumberPagination?
Question 3
Why is max_page_size critical on custom PageNumberPagination classes?
Question 4
Why does CursorPagination omit the total count property from its JSON response?
Question 5
How can a client disable pagination for a specific request if allowed by the view?
Next Lesson

One-to-One, One-to-Many, Many-to-Many

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Search & Query ParametersOne-to-One, One-to-Many, Many-to-Many

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext