Assuming you have already done the the tutorial P6 – Django – Viewsets it is possible that we might need pagination on some of the rest calls. So we may have a lot of data on the server and sending it all at once back to the client may not be required and may not be the efficient way to handle the data. The client on the other hand might need only a handful of data and would not want all the data which will become difficult to process. So Lets see how do we set up pagination on view sets.
lets take example of rest services which returns “Ads” and it may have thousands of ads. At any point of time the client will need only a handful of them to process. So the client must be able to pass the rest service some parameter identifying the page number it desires.
For our example. We create a pagination.py file and add below content.
from rest_framework.pagination import PageNumberPagination
class PaginationMeta(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 10000
Here we have set three parameters. One is the “page_size” which means the amount of data the server will send if requested with page number i.e with parameter “page” . The next is “page_size_query_param” which can be used if want a specified number of records to be retrieved and override the page_size. “max_page_size” which is the max page size for our request.
The next step is to set this class in our viewset as below.
from django.shortcuts import render
from .models import Ads
from .serializers import AdsSerializer
from rest_framework import viewsets
from .pagination import PaginationMeta
# Create your views here.
class AdsView(viewsets.ModelViewSet):
queryset = Ads.objects.all()
serializer_class = AdsSerializer
pagination_class = PaginationMeta
Here we set the pagination_class to the PaginationMeta we created. And this is all we will have to do to set up pagination. I am providing other classes required for this example.
from django.db import models
# Create your models here.
class Ads(models.Model):
category = models.CharField(max_length=50)
city= models.CharField(max_length=50)
email = models.CharField(max_length=50)
brand = models.CharField(max_length=50)
make = models.CharField(max_length=50)
capacity = models.CharField(max_length=50)
desc = models.CharField(max_length=150)
price = models.FloatField(max_length=20)
from rest_framework import serializers
from .models import Ads
class AdsSerializer(serializers.ModelSerializer):
class Meta:
model = Ads
fields = ['id','category', 'city','email','brand','make','capacity','price','desc']
from rest_framework.routers import DefaultRouter
from . import views
from django.urls import path, include
router = DefaultRouter()
router.register('ads', views.AdsView)
urlpatterns = [
path('', include(router.urls))
]
Now we have set the url pattern for our views to “/ads” so to access the views so a call to http://localhost:8000/api/ads/ will return the first page as below.
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 17,
"next": "http://localhost:8000/api/ads/?page=2",
"previous": null,
"results": [
{
"id": 1,
"category": "Cars & MotorCycles",
"city": "Pune",
"email": "",
"brand": "MARUTI",
"make": "Brezza",
"capacity": "1.2",
"price": 123213.0,
"desc": "dadsadsad"
},
{
"id": 2,
"category": "Cars & MotorCycles",
"city": "Pune",
"email": "",
"brand": "HYUNDAI",
"make": "I20",
"capacity": "1.2",
"price": 34324234.0,
"desc": "asdsadsads"
},
{
"id": 3,
"category": "Cars & MotorCycles",
"city": "Pune",
"email": "",
"brand": "MARUTI",
"make": "Brezza",
"capacity": "1.2",
"price": 122132.0,
"desc": "sadsadad"
}
]
}
As you can see, it has returned with the first page and also various others attributes to navigate to next page. We can jump to any page we wish by providing the page parameter to the url e.g. http://localhost:8000/api/ads/?page=4
Please let us know if you liked this post and if it helped you. Thank you for spending time reading my post.