>电气民用
我正在尝试为我的大学做一个测验应用程序…我想把每个系都映射到一个院系
models.py
Class Faculty(models.Model):
faculty_choice = [
("FOE","faculty of engineering"),
("FOS","faculty of science"),
("FOA","faculty of Agriculture"),
]
faculty = models.CharField(
max_length=3,
choices=faculty_choice,
default=FOE,
)
Class Department(models.Model):
Faculty = models.FOREIGNKEY( Faculty, on_delete=CASCADE)
#this is where my problem is I don't know the next step
在工程学院,我们有其他的选择
工程学院
机械理学院
数学- 物理学化学
农学院
- 植物
- 经济与扩展
希望我的问题足够有描述性❓
你做的一切都是正确的,只要在你的Department
模型中创建department_name
:
faculty_choice = [
("FOE", "faculty of engineering"),
("FOS", "faculty of science"),
("FOA", "faculty of Agriculture"),
]
class Faculty(models.Model):
faculty_name = models.CharField(choices=faculty_choice, default='FOE')
class Department(models.Model):
department_name = models.CharField(max_length=150)
faculty = models.ForeignKey(Faculty, on_delete=models.CASCADE, related_name='departments')
则可以从Faculty
类对象中使用related_name
访问部门字段。假设foe
是教员对象,使用:
foe.departments.all()
将给出与foe
有关系的Department
模型的所有实例