1- from rest_framework import generics , permissions
2-
1+ from rest_framework import generics , permissions , pagination
2+ from rest_framework . response import Response
33
44from .models import Post
55from .permissions import IsOwnerOrReadOnly
66from .serializers import PostSerializer
77
8+ class PostPageNumberPagination (pagination .PageNumberPagination ):
9+ page_size = 5
10+ page_size_query_param = 'size'
11+ max_page_size = 20
12+
13+ def get_paginated_response (self , data ):
14+ author = False
15+ user = self .request .user
16+ if user .is_authenticated :
17+ author = True
18+ context = {
19+ 'next' : self .get_next_link (),
20+ 'previous' : self .get_previous_link (),
21+ 'count' : self .page .paginator .count ,
22+ 'author' : author ,
23+ 'results' : data ,
24+ }
25+ return Response (context )
26+
827
928
1029class PostDetailAPIView (generics .RetrieveUpdateDestroyAPIView ):
@@ -23,6 +42,7 @@ class PostListCreateAPIView(generics.ListCreateAPIView):
2342 queryset = Post .objects .all ()
2443 serializer_class = PostSerializer
2544 permission_classes = [permissions .IsAuthenticatedOrReadOnly ]
45+ pagination_class = PostPageNumberPagination
2646
2747 def perform_create (self , serializer ):
2848 serializer .save (user = self .request .user )
0 commit comments