Reputation: 452
(Capitalized the objects within the process).
Is this considered legal, my goal is to have 3 individual objects (the reasoning behind this is to allow me to assign a specific school + major to specific professors). When a user is to select a SCHOOL, I want my website to pull the MAJORS that specifically correspond to that SCHOOL, and when they select a MAJOR, I want the specific PROFESSORS to display.
Logical connection between the objects below (business rules)
The SCHOOL needs to be a many-to-many relationship with MAJOR:
- One SCHOOLS can have MANY Majors
- One Major can have MANY SCHOOLS
The PROFESSOR
- a PROFESSOR can work at ONE SCHOOL, and teach ONE MAJOR
- a Major can be taught by MANY PROFESSORS, at MANY SCHOOLS
So my difficulty is writing out the logic of the models to ensure all conditions are met, the information set before the rules is the experience I want my users to have when it comes to information pulled from the database to the website, and the logic below it.
from django.db import models
class Professor(models.Model):
School = models.ForeignKey(School , on_delete=models.CASCADE)
Major = models.ForeignKey(Major , on_delete=models.CASCADE)
class Major(models.Model):
Major = models.CharField(max_length=30)
School = models.ForeignKey(School , on_delete=models.CASCADE)
class School(models.Model):
School = models.Charfield(max_length=50)
Also, if you have any recommendations on making this logic more clear, I would grately appreciate it!
Upvotes: 0
Views: 73
Reputation: 3574
Try using many-to-many fields for the relationship between schools and majors. I would add it onto the school since I personally think about it as "Schools have many majors" more frequently than "Majors are taught at many schools" but both would be fine.
Also, set the text fields to be something like name
since that's what the text field represents: not the school/major, but rather the name of the school/major. Other than that, your logic looks good
from django.db import models
class Major(models.Model):
name = models.CharField(max_length=30, db_index=True)
class School(models.Model):
name = models.Charfield(max_length=50, db_index=True)
majors = models.ManyToManyField(Major)
class Professor(models.Model):
school = models.ForeignKey(School , on_delete=models.CASCADE)
major = models.ForeignKey(Major , on_delete=models.CASCADE)
Once you've got this, you can grab major objects and add them to schools
major = Major.objects.get(name='Physics')
school = School.objects.get(name='Harvard')
school.majors.add(major)
...or grab schools and add them to majors via reverse-lookup (the "_set" attribute):
school = School.objects.get(name='Harvard')
major = Major.objects.get(name='Physics')
major.school_set.add(school)
Professors would be created without the ManyToMany relationships
school = School.objects.get(name='Harvard')
major = Major.objects.get(name='Physics')
Professor.objects.create(school=school, major=major)
Documentation reference on ManyToManyField: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/
Upvotes: 1