# Models question

**URL:** <https://forum.djangoproject.com/t/models-question/29518>\
**Category:** Using the ORM\
**Created:** [March 26, 2024, 6:12pm UTC](https://forum.djangoproject.com/t/models-question/29518 "2024-03-26T18:12:48Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 26, 2024, 6:52pm UTC](https://forum.djangoproject.com/t/models-question/29518/2 "2024-03-26T18:52:00Z")

</div>

> [@qvisty](#):
>
> I am planning the following models and my question will be about how “deeply nested” the foreign key relationships can go logically.

They can go as deep as you need them to.

If you have the ForeignKeys defined as having the same name as the target class, and you have an instance of `School` named `school`, then the query for all `Student` in that school would be `Student.objects.filter(schoolclass __team__ departmening__school=school)`

> [@qvisty](#):
>
> Is this “multiple nesting” best practice or is there a better way.

This is _a_ “best practice”, but not the only viable option. There are other ways, but whether or not they’re “better” is both subjective and dependant upon the operational characteristics of the data. (These characteristics to consider include the sizes of the individual levels, the frequency of updates to the data, what the distribution of those updates are among different insert, updates, and deletions of each of the levels, and what the common operations are - what the common queries are expected to be relative to the uncommon queries - and what the relative number of queries are going to be performed to the number of updates.)

Another option to consider would be one of the hierarchical representations for this data. This structure _is_ a hierarchy, with the characteristic of the number of levels being fixed and given. (See [Some modelling advice - #12 by KenWhitesell](https://forum.djangoproject.com/t/some-modelling-advice/3284/12) for more thoughts on this.) If I were to make a guess as to some of the answers above, I could see where either a Nested Set Tree or a Materialized Path Tree could be quite useful for this.

---

_[View the full topic](https://forum.djangoproject.com/t/models-question/29518)._
