I'm trying to integrate support for an external library in my project. The external library requires a precise data structure that it uses to call response-as-a-table.
A simple serializer for my model, could be:
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('id', 'title', 'author')
So, supposing a snippet like this:
queryset = Book.objects.all()
serializer = BookSerializer(queryset, many=True)
serializer.data
Which gives this output:
[
{'id': 0, 'title': 'The electric kool-aid acid test', 'author': 'Tom Wolfe'},
{'id': 1, 'title': 'If this is a man', 'author': 'Primo Levi'},
{'id': 2, 'title': 'The wind-up bird chronicle', 'author': 'Haruki Murakami'}
]
How should I reshape my BookSerializer class to achieve this result? I can't figure it out.
{
'id': [0, 1, 2],
'title': ['The electric kool-aid acid test', 'If this is a man', 'The wind-up bird chronicle'],
'author': ['Tom Wolfe', 'Primo Levi', 'Haruki Murakami']
}