We are using Django Treebeard's materialized path to model an organizational hierarchy as follows:
Now each node in the organizational tree can have multiple tasks:
class Organization(MP_Node):
node_order_by = ['name']
name = models.CharField(max_length=100)
class Task(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
description= models.TextField()
Given a list of tasks, we wish to include the full organizational path of each task in the result. How can we achieve this without the need for N+1 queries?
Expected result for organization Factory 1 could be for example:
Task name | Organization Path |
---|---|
Task 1 | MyCompany/Factory 1/Maintenance |
Task 2 | MyCompany/Factory 1/Operations |
Task 3 | MyCompany/Factory 1 |
Task 4 | MyCompany/Factory 1/Operations |