I'm using the app django-mptt to store a small tree structure. In django view, how can I query the entire table in a single query, to get a queryset/list with each instance having a children attribute?
So I would like to do:
groups = SongGroup.objects.all().values()
for group in groups:
group['children'] # list of children
But as I see in the docs, get_children() will trigger another query? Which I want to avoid, since I've already used all() to query the entire table.
I couldn't find a way in the docs to get an actual tree like structure from the table.
My model:
class SongGroup(MPTTModel):
song = models.ForeignKey(Song, on_delete=models.CASCADE)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)