Add: Pagination to PostListView

This commit is contained in:
Aroy-Art 2025-03-12 22:22:33 +01:00
parent a8c3a67230
commit 163a3e3b6f
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -6,12 +6,32 @@ from apps.archive.models import PostModel
from .serializers import PostPreviewSerializer, PostSerializer
from rest_framework.pagination import PageNumberPagination
class PostListPagination(PageNumberPagination):
page_size = 10 # number of items per page
page_size_query_param = "page_size" # allows clients to specify page size
max_page_size = 100 # maximum page size allowed
def get_paginated_response(self, data):
return Response(
{
"count": self.page.paginator.count,
"totalPages": self.page.paginator.num_pages, # total number of pages
"next": self.get_next_link(),
"previous": self.get_previous_link(),
"results": data,
}
)
class PostListView(ListAPIView):
permission_classes = [IsAuthenticated]
serializer_class = (
PostPreviewSerializer # Each post will be serialized using this serializer
)
pagination_class = PostListPagination
def get_queryset(self):
user = self.request.user.userprofile