From 7774d5639008e792401c642ba41f6a3885034613 Mon Sep 17 00:00:00 2001 From: Kushagra Gupta Date: Sun, 8 Jun 2025 03:19:53 +0530 Subject: [PATCH] Adds pagination to the products api endpoint --- backend/core/settings/base.py | 2 ++ backend/products/views.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/backend/core/settings/base.py b/backend/core/settings/base.py index 5815103..4fffb4d 100644 --- a/backend/core/settings/base.py +++ b/backend/core/settings/base.py @@ -53,6 +53,8 @@ 'rest_framework.filters.SearchFilter', 'rest_framework.filters.OrderingFilter', ], + 'DEFAULT_PAGINATION_CLASS' : 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 10, } MIDDLEWARE = [ diff --git a/backend/products/views.py b/backend/products/views.py index ce675fa..403007d 100644 --- a/backend/products/views.py +++ b/backend/products/views.py @@ -1,11 +1,17 @@ from rest_framework.generics import ListAPIView, RetrieveAPIView from rest_framework.filters import SearchFilter, OrderingFilter +from rest_framework.pagination import PageNumberPagination from django_filters.rest_framework import DjangoFilterBackend from django_filters import rest_framework as filters from .models import Product from .serializers import ProductSerializer +class ProductPagination(PageNumberPagination): + page_size = 15 + page_size_query_param = 'page_size' + max_page_size = 100 + class ProductFilter(filters.FilterSet): # simple text filters name = filters.CharFilter(lookup_expr='icontains') @@ -31,6 +37,7 @@ class ListProductView(ListAPIView): serializer_class = ProductSerializer filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter] filterset_class = ProductFilter + pagination_class = ProductPagination search_fields = ['name', 'description', 'tags', 'seller', 'type']