Home > front end >  my pagination seems not to be working - DRF problem
my pagination seems not to be working - DRF problem

Time:01-28

I am trying to create an endpoint that returns a list of posts. I want lets say 2 posts per page (for testing only! I know its not that big of a number to cause problem!). here is my views.py

class blogsViewSet(ModelViewSet):
    queryset = Posts.objects.all()
    serializer_class = PostSerializer
    pagination_class = pagination.CustomPagination

    
    def list(self, request):
        data = request.data
        uid = data['uid']
        context = {"user": uid}
        blogs = Posts.objects.all()
        serializer = PostSerializer(blogs, many= True, context= context)
        return Response(serializer.data)

here is my serializers.py

class PostSerializer(ModelSerializer):    
    isLiked = serializers.SerializerMethodField(method_name='check_react')
    totalLikes = serializers.SerializerMethodField(method_name='get_likes')
    totalComments = serializers.SerializerMethodField(method_name='get_comments')

    def check_react(self, post):
        userObj = Users.objects.get(userID = self.context['user'])
        #print(type(userObj))
        
        if Likes.objects.filter(postID = post, userID = userObj).exists():
            isLiked = Likes.objects.get(postID = post, userID = userObj)
            likeObj = LikeSerializer(isLiked)
            #print('isLiked: ', likeObj.data['isLiked'])
            return (likeObj.data['isLiked'])
        return(False)
        #print(isLiked)
    
    def get_likes(self, post):
        count = Likes.objects.filter(postID = post).count()
        return count

    def get_comments(self, post):
        count = PostsComments.objects.filter(postID = post).count()
        return count

    class Meta:
        model = Posts
        fields = '__all__'

and, here is my pagination.py,

from rest_framework import pagination

class CustomPagination(pagination.PageNumberPagination):
    page_size = 2
    page_size_query_param = 'page_size'
    max_page_size = 3
    page_query_param = 'p'

I am importing this class on views.py and it works as expected when I try to retrieve a list of users via userMVS

class userMVS(ModelViewSet):
    queryset = Users.objects.all()
    serializer_class = UserSerializer
    pagination_class = pagination.CustomPagination

CodePudding user response:

You must write your list function of views as -

def list(self, request, *args, **kwargs):
    queryset = Posts.objects.all()
    paginatedResult = self.paginate_queryset(queryset)
    serializer = self.get_serializer(paginatedResult, many=True)
    return Response(serializer.data)

Edits -

Suppose this is my pagination class -

class CustomPagination(PageNumberPagination):
    page_size = 8
    page_query_param = "pageNumber"

and used in this way at class level and mentioned above at list function -

class blogsViewSet(ModelViewSet):
    queryset = Posts.objects.all()
    serializer_class = PostSerializer
    pagination_class = CustomPagination

then this is the url that should work

http://localhost:8000/urlofpostviewset/?pageNumber=passThePageNumber

Do read from the documents all the properties you use at CustomPagination class.

  •  Tags:  
  • Related