I have 3 models where model A has foreign key of another in reverse order like:
class Book(models.Model):
name = models.CharField(max_length=100)
img=models.CharField(blank=True)
category=models.CharField(max_length=100,null=True)
class Section(models.Model):
book= models.ForeignKey(Book, related_name='sections', on_delete=models.PROTECT)
title= models.TextField(null=True)
class SubSection(models.Model):
section=models.ForeignKey(Section, related_name='subSections',
on_delete=models.PROTECT, null=True)
sub_title= models.TextField(null=True)
I am trying to fetch all sections and subsections on the basis of book id. Before I was using nested serializer but nested serializer slow down its response. i am trying to achieve it with select_related can anyone help me with view query and serializer class. I want response like:
data=[
"section": "A",
"title": "intro",
"subsection": [
{
"id": 1,
"sub_title": "title"
}
]
]