Since the PaginationSerializer was removed in DRF 3.1, you must implement your own logic, for more information, refer to: fooobar.com/questions/735249 / ...
So, you need to modify your BookSerializer to enable pagination behavior as follows:
Bookseerializer
class BookSerializer(ModelSerializer): wordinbook_set = serializers.SerializerMethodField('paginated_wordinbook') class Meta: model = Book fields = ('id', 'title', 'author', 'wordinbook_set') def paginated_wordinbook(self, obj): page_size = self.context['request'].query_params.get('size') or 10 paginator = Paginator(obj.wordinbook_set.all(), page_size) page = self.context['request'].query_params.get('page') or 1 words_in_book = paginator.page(page) serializer = WordInBookSerializer(words_in_book, many=True) return serializer.data
First, you need to use the Paginator found in django.core.paginator to break down the iterative object:
paginator = Paginator(obj.wordinbook_set.all(), page_size)
Then get the landing page from the paginated data:
words_in_book = paginator.page(page)
Serialize pagination with set = True:
serializer = WordInBookSerializer(words_in_book, many=True)
To make a dynamic page size, you can use query_params to get the desired page size, for example, you can choose a page size of 10 in the request and be 100 in another request to get the page size:
page_size = self.context['request'].query_params.get('size') or 10
Finally, to allow the user to request a specific page, use query_params again to retrieve it:
page = self.context['request'].query_params.get('page') or 1