Django model relations [nesting and hierarchy]

Viewed 169

I apologize in advance for the novice question and if it is already answered. I have searched multiple terms without getting a clear answer.

I'm trying to figure out a model schema and it's relationships in a Django project.

The questions that arise while doing this are as follows:

Regarding the first part of the code: (Country > State > City > University > Department > Subject):

  • Are there too many levels of hierarchy? Any way to model it better or more efficiently?

    • I have been looking at nested relationships as well (serializers), but no clue if there's a better way to organize it.
  • Is it a good practice to have models such as State or City with only one field in the hierarchy?

    • I mean, I need them, but not sure if they are avoidable due to their simplicity with another models relationship solution.
  • Can a "model" access its second/third/nth parent? Eg: A subject can access easily its department via:

    Subject.objects.filter(department__name='Maths')

    But is it possible to access the University or State from the subject object? How?
    ** Already answered by daneil James

Regarding the second part of the code (Person and laptop):

  • I have a few roles in the models (president, dean, director, teacher). Some of those fields have a ManyToMany relation:
    • Eg: A teacher can be in more than one subject and a subject can have more than a teacher.

I was assuming that I could use the built-in users API and roles/groups for this matter. So at the end, president or teacher, are roles, but no idea how to implement it in that way or if it's a good idea.

  • Regarding the laptops, let's say that the university has some laptops (ManyToOne relation), that's straightforward.
    • But those laptops are assigned to a department and a subject. Do I need a ForeignKey for University, Department and Subject in the laptop model?

How would it be possible to count them properly or have a registry? University has 10 laptops, assigned to 2 departments and 5 subjects.

Again, I apologize if the questions are too basic.

Thanks in advance!


Model Schema: (pseudo-code)

class Country(models.Model):
    name: 
    extension:
    currency:
    <<president>>:

class State (models.Model)
    name:
    **country: models.ForeignKey(Country)**

class City (models.Model)
   name:
   **state: models.ForeignKey(State)**

class University(models.Model)
    name:
    address:
    <<dean>>:
    **city: models.ForeignKey(City)**
    laptop:

class Department(models.Model)
    name:
    <<director>>:
    **university: models.ForeignKey(University)**
    laptop:

class Subject(models.Model)
    name:
    description:
    <<teacher>>:
    **department: models.ForeignKey(Department)**
    laptop:

class Person(models.Model):
    name: 
    address:
    age:

class Laptop(models.Model):
    brand:
    model:
    serial:
    date:
    
1 Answers

Regarding Can a "model" access its second/third/nth parent? Eg: A subject can access easily its department via: Yes you can follow backward and forward relationship in django

`Subject.objects.filter(department__university__city__country = 'Your query')`

If relation is one to many at end of parent you could use _set

`Subject.objects.filter(department__university__city__country_set = 'Your query')`
Related