Add: Pagination to PostListView
This commit is contained in:
parent
a8c3a67230
commit
163a3e3b6f
1 changed files with 20 additions and 0 deletions
|
@ -6,12 +6,32 @@ from apps.archive.models import PostModel
|
||||||
|
|
||||||
from .serializers import PostPreviewSerializer, PostSerializer
|
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):
|
class PostListView(ListAPIView):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
serializer_class = (
|
serializer_class = (
|
||||||
PostPreviewSerializer # Each post will be serialized using this serializer
|
PostPreviewSerializer # Each post will be serialized using this serializer
|
||||||
)
|
)
|
||||||
|
pagination_class = PostListPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
user = self.request.user.userprofile
|
user = self.request.user.userprofile
|
||||||
|
|
Loading…
Add table
Reference in a new issue