# Organisation Hierarchy Model

**URL:** <https://forum.djangoproject.com/t/organisation-hierarchy-model/17085>\
**Category:** Using the ORM\
**Created:** [November 17, 2022, 2:29am UTC](https://forum.djangoproject.com/t/organisation-hierarchy-model/17085 "2022-11-17T02:29:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![csaba911](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/csaba911/32/3378_2.png) [@csaba911](https://forum.djangoproject.com/u/csaba911)\
**Post date:** [November 17, 2022, 2:29am UTC](https://forum.djangoproject.com/t/organisation-hierarchy-model/17085/1 "2022-11-17T02:29:08Z")

</div>

Looking for input on modeling.  
Current users model have a field `title` foreignKey to separate model and `reports_to` charfiled what I’m planning to use to create/display a hierarchy.  
Im sure the image below explain exactly what the output should be.  
CEO-\>Regional Managers-\>Supervisors-\>Team Leads-\>Employees  
Working on multiple updates currently so Im not thinking straight 😁  
Is the `title` alone enough to achieve this ?  
Should `reports_to` point to a foreignKey `title` ? or a `title` should take responsibility to other `title`’s ?  
Did a bit of search on this but no solid example found, any input would be great especially with examples or db diagram.

Thanks

 ![oh](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/f/fd393aadb62a9772ad780a953d2ec09492c9b546.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 17, 2022, 2:38am UTC](https://forum.djangoproject.com/t/organisation-hierarchy-model/17085/2 "2022-11-17T02:38:54Z")

</div>

You can search around this forum for “hierarchy” to see some other discussions on this topic.  
(See [Some modelling advice - #12 by KenWhitesell](https://forum.djangoproject.com/t/some-modelling-advice/3284/12), and the external link referenced in that comment.)

In general, there are at least three different mechanisms for representing a hierarchical data structure in a relational database effectively. They all require additional data beyond a Foreign Key relationship.

Personally, I recommend [treebeard](https://django-treebeard.readthedocs.io/en/latest/), because it implements all three of the common algorithms.

---

<div class="post-metadata">

**Author:** ![csaba911](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/csaba911/32/3378_2.png) [@csaba911](https://forum.djangoproject.com/u/csaba911)\
**Post date:** [November 17, 2022, 5:45am UTC](https://forum.djangoproject.com/t/organisation-hierarchy-model/17085/3 "2022-11-17T05:45:19Z")

</div>

Might be making a mistake here 🤔 but I run some testing with my initial idea of

```auto
class Title(BaseModel):
    title = models.CharField(_("Title"), max_length=50)

class Employee(AbstractUser):
    title = models.ManyToManyField(Title, verbose_name=_("Title"))
    type = models.ForeignKey(EmploymentType, _("Employment Type"), on_delete=models.CASCADE)
    base = models.ForeignKey(Base, _("Base"), on_delete=models.CASCADE, blank=True, null=True)
    office = models.ForeignKey(Office, _("Office"), on_delete=models.CASCADE, blank=True, null=True)
    report_to_role = models.ForeignKey(Title, _("Report to Role"), related_name="report_to_role", on_delete=models.CASCADE, null=True)

# --------------------Get Coupers title = Field Tech ------------------------------------------
queryset.filter(last_name='Couper').values_list('title')
<QuerySet [(1,)]>
# --------------------Field Tech reports to Regional Manager pk:5-----------------------
queryset.filter(title=1).values_list('report_to_role__title')
 <QuerySet [('Regional Manager',)]>
# --------------------Get Everyone who's title is Regional Manager ---------------------
queryset.filter(title='5')
<QuerySet [<Employee: Big Boss>]>

```

In my test project I only have 3 user, maybe should add 10-20 to see the different relations
