Now lets say, you want to paginate your data. You can use the PageNumberPagination that comes with rest_framework.pagination. And then you need to create an instance of the PageNumberPagination class. Finally, you go to your settings.py file and where the rest_framework settings is, you set your page size there
from rest_framework.pagination import PageNumberPagination
class ProductViewSet(ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter] filterset_class = ProductFilter pagination_class = PageNumberPaginationn # remove this when you set global pagination search_fields = ['title', 'description'] ordering_fields = ['unit_price', 'last_update']REST_FRAMEWORK = { 'COERCE_DECIMAL_TO_STRING': False, 'PAGE_SIZE': 10}The page size being 10 will return 10 products on each page, and the next page, and it goes on and on until all the products have been returned.
But the problem about this method of pagination is that it only works for our products. Now what if we wanted to paginate our collections. We can actually set the pagination globally in our settings.py file. see below
REST_FRAMEWORK = { 'COERCE_DECIMAL_TO_STRING': False, 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE': 10}By doing this, we dont need to set the Pagination per class, it will work for everything.
But when we specify a PAGE_SIZE without specifying a default_pagination_class, we get an error that says..
WARNINGS:?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting, without specifying also a DEFAULT_PAGINATION_CLASS. HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.There are two ways to get rid of this error, the first one is to silence the error, and the second one is by setting a default pagination class. This is done by creating a “pagination.py” file, and then creating a pagination class and set the page size there. see below
from rest_framework.pagination import PageNumberPagination
class DefaultPagination(PageNumberPagination): page_size = 10And then in your views.py, instead of creating an instance of the PageNumberPagination class create an instance of the DefaultPagination class you just created
from .pagination import DefaultPagination
class ProductViewSet(ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter] filterset_class = ProductFilter pagination_class = DefaultPagination # over here search_fields = ['title', 'description'] ordering_fields = ['unit_price', 'last_update']And then go to your settings.py file, and remove the page_size from there. so it will become something like this
REST_FRAMEWORK = { 'COERCE_DECIMAL_TO_STRING': False,}