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.