我是Django的新手,并试图建立一个教师与学生关系的模型。教师和学生都可以访问任何特定课程的课程材料。教师将在课程中有许多学生,每个学生将有许多教师(取决于他们正在参加多少课程)。我现在有这个设置:我是否在正确的轨道上?
class CourseWare(models.Model):
#information goes here?
instructor = models.ForeignKey(Instructor)
TrainingManager = models.ForeignKey(TrainingManager)
class Instructor(models.Model):
firstName = models.CharField(max_length=30, blank=True, null=True)
lastName = models.CharField(max_length=30, blank=True, null=True)
material = models.ForeignKey(courseWare) #instructor deals with one course
def __unicode__(self):
return u's_' + self.firstName + u' ' + self.lastName
class Student(models.Model):
firstName = models.CharField(max_length=30, blank=True, null=True)
lastName = models.CharField(max_length=30, blank=True,null=True)
instructors = models.ManyToManyField(Instructor) #Many Instructors <-> Many Students?
material = models.ForeignKey(CourseWare)
terminationDate = models.DateField()
答案 0 :(得分:1)
由于你的教师只能教一门课程,我会做这样的事情:
class Course(models.Model):
instructor = models.OneToOneField(Instructor, related_name='course')
training_manager = models.ForeignKey(TrainingManager)
students = models.ManyToManyField(Course, related_name='courses')
class Instructor(models.Model):
first_name = models.CharField(max_length=30, blank=True, null=True)
last_name = models.CharField(max_length=30, blank=True, null=True)
class Student(models.Model):
first_name = models.CharField(max_length=30, blank=True, null=True)
last_name = models.CharField(max_length=30, blank=True, null=True)
termination_date = models.DateField()
related_name
关键字参数允许您反向查询。例如,some_student.courses.all()
。