I have two models:
class Book(models.Model):
title = models.CharField(max_length=250)
author = models.CharField(max_length=250)
class WordInBook(models.Model):
book = models.ForeignKey("Book")
word = models.ForeignKey("Word")
And corresponding serializers:
class BookSerializer(ModelSerializer):
wordinbook_set = WordInBookSerializer(many=True)
class Meta:
model = Book
fields = ('id', 'title', 'author', 'wordinbook_set')
class WordInBookSerializer(ModelSerializer):
class Meta:
model = WordInBook
fields = ('word')
Now I want to paginate the wordinbook_set. Outside of serialiser it is easy:
book = Book.objects.get(pk=book_id)
paginator = Paginator(book.wordinbook_set.all(), 10)
words = paginator.page(page).object_list
But that leaves me with two separate serialized objects.
Question: how do I paginate wordinbook_set inside the serializer?
The resulting json should look like this:
{id: '...', title: '...', author: '...', wordinbook_set: [ 10 WordInBook objects here ]}