Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

database - Django model relations [nesting and hierarchy]

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

I'm trying to figure out a model schema and it's relations in a Django project. The questions that arise while doing that are the following:

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 to the 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 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:
    
question from:https://stackoverflow.com/questions/65902322/django-model-relations-nesting-and-hierarchy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...