I've two Django models with a foreign key relationship:
class Parent(models.Model):
id = models.IntegerField()
parent_name = models.CharField()
class Child(models.Model)
id = models.IntegerField()
child_name = models.CharField()
parent = models.ForeignKey(Parent, related_name='parent', on_delete=models.CASCADE)
Both tables are high volume so looking for something very efficient. What's the best way to write an efficient django view to return json as:
Expected JSON:
[
{
"id": 1,
"parent_name": "Elon",
"childs":
[
{
"id": 101,
"child_name": "Elon son"
},
{
"id": 101,
"child_name": "Elon daughter"
}
]
},
{
"id": 2,
"parent_name": "Jeff",
"childs":
[
{
"id": 101,
"child_name": "Jeff son"
},
{
"id": 101,
"child_name": "Jeff daughter"
}
]
}
]