SilentDev
SilentDev

Reputation: 22747

DjangoRestFramework - How to serialize only certain number of objects when ViewSet GET / LIST is called?

So this is my ViewSet:

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (IsAuthenticated, IsLikeOrOwnerDeleteOrReadOnly,)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user, location=self.request.user.userextended.location)

    @detail_route(methods=['post'], permission_classes=[IsFromLoactionOrReadOnly])
    def like(self, request, pk=None):
        post = self.get_object()
        post.usersVoted.add(request.user)
        return Response(status=status.HTTP_204_NO_CONTENT)

and this is my URL / router:

router.register(r'posts', views.PostViewSet)

Now, when I go to this URL:

/posts

DRF sends all the posts and serializers (or so I think.. I don't have many posts yet so I'm assuming it sends all of them). What I want to do is I want to be able to limit the number of posts my ViewSet serializes to 10. The 10 objects I want to serialize depends on the page number which I want to force API users to send with the URL. For example, I want to force users to send a number with the URL like so:

/posts/x

and on the backend, I want to serialize posts with the pk x to x+9 (so if we assume x=1, then I want to serialize posts with pk=1, pk=2, pk=3... pk=10.). Is this possible with DRF? I'm guessing I use Pagination because when I read the documentation, it kind of looks like what I need but I can't fully wrap my head around what pagination exactly is and how I can use it to accomplish what I want. Here is the documentation for pagination: http://www.django-rest-framework.org/api-guide/pagination/

Thanks in advance.

Upvotes: 1

Views: 460

Answers (2)

Abhishek
Abhishek

Reputation: 2673

You are right. You need Pagination to achieve this. Just include pagination_class like you wrote serializer_class etc. Also, create a class which will have the number, with which you wish to paginate.

from rest_framework.pagination import PageNumberPagination
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 1000

and set pagniation_class = StandardResultsSetPagination.

Upvotes: 1

Adam_O
Adam_O

Reputation: 341

You'll want to use the pagination controls that Django Rest Framework provides just like you guessed.

This snippit might help you wrap your head around it:

{
    "count": 1023
    "next": "https://api.example.org/accounts/?page=5",
    "previous": "https://api.example.org/accounts/?page=3",
    "results": [
       …
    ]
}

It is showing a response that, in addition to the normal 'results' that you would expect also includes next and previous values. These are links to the end points that can be used to return the results for the previous page worth of data, or the next page worth of data. It is the front end's responsibility to map these links to the appropriate user controls so that the pagination navigation can occur.

Upvotes: 1

Related Questions