We have seen in those examples (ex_1, ex_2) how to use and override the pagination classes in any generic class base view.
What happens when we want to use pagination in a function based view?
Lets also assume that we want to create a function based view for MyModel
with PageNumberPagination
, responding only to a GET
request. Then:
from rest_framework.pagination import PageNumberPagination
@api_view(['GET',])
def my_function_based_list_view(request):
paginator = PageNumberPagination()
query_set = MyModel.objects.all()
context = paginator.paginate_queryset(query_set, request)
serializer = MyModelSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
paginator = PageNumberPagination()
to this
paginator = MyCustomPagination()
provided that we have defined MyCustomPagination
to override some default pagination