I have a ManyToMany
field in my models. In serializer, I am able to get the nested serialized data, but I want to normalize it.
models.py
class Authors(models.Model):
name = models.CharField(max_length=20)
class Mets:
db_table = "authors"
class Books(models.Model):
book_name = models.CharField(max_length=100)
authors = models.ManyToManyField(Authors, related_names="books")
class Meta:
db_table = "books"
serializers.py
class AuthorSerializer(serializer.ModelSerializer):
name = serializer.CharField()
class Meta:
model = Authors
field = ("name",)
class BooksSerializer(serializer.ModelSerializer):
authors = AuthorSerializer()
class Meta:
model = Books
field = ("book_name", "authors")
The output of the above will be:
"result": [
{
"book_name": "Sample",
"authors": [
{
"name": "Person 1",
},
{
"name": "Person 2",
}
]
}
]
But I want to output something like this:
"result": [
{
"book_name": "Sample",
"author_name": "Person 1",
},
{
"book_name": "Sample",
"author_name": "Person 2",
},
]
UPDATE
views.py
class ReportViewSet(XLSXFileMixin, viewsets.ReadOnlyModelViewSet):
serializer_class = BookSerializer
renderer_classes = [XLSXRenderer]
filename = 'my_export.xlsx'
def get_queryset(self):
queryset = Book.objects.all()
book_id = self.request.query_params.get("pk", None)
if book_id is not None:
queryset = queryset.filter(id=book_id)
return queryset
The excel report should have columns: book_name
, author_name
But with the solution given by @Klim Bim, I get an empty excel report with just column names: book_name
and authors
.
Thanks in advance.