Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to add pagination in Django REST framework??

from rest_framework.response import Response
from .serializers import BlogGetSerializer
from rest_framework.views import APIView
from .models import Blog
from rest_framework.pagination import PageNumberPagination

# my blog list
class MyBlogList(APIView, PageNumberPagination):
    # this will output only 5 objects per page
    page_size = 5

    def get(self, request):
        myBlogList = Blog.objects.get()
        # paginate them
        results = self.paginate_queryset(myBlogList, request, view=self)
        serializer = BlogGetSerializer(results, many=True)
        # send paginated data
        return self.get_paginated_response(serializer.data)
        
# API => http://127.0.0.1:8000/blog/details/2/
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #add #pagination #Django #REST
ADD COMMENT
Topic
Name
4+5 =