使用自定义中间媒介来实现多对多关系的时候,主要的目的是在中间媒介中保存关系的特有属性。举个例子,比如学生与课程之间的关系就属于多对多的,而且当学生与课程建立关系之后随之还会产生附属的属性,如:成绩。
如下例:
1<!-- lang: python --> 2class Student(models.Model): 3 name = models.CharField(max_length=5) 4 cources = models.ManyToManyField(Course, through='StudentCourse') 5class Course(models.Model): 6 name = models.CharField() 7class StudentCourse(models.Model): 8 student = models.ForeignKey(Student) 9 course = models.ForeignKey(Course) 10 score=models.FloatField()
正常的情况下,我们期望能够通过如下方式访问到学生的所选课程及成绩:
1<!-- lang: python ---> 2cources = Student.objects.get(id=1).cources 3for c in cources: 4 print 'Course:%s Score:%s\n' % (c.name,c.score)
实际的表现是,会产生一个运行期错误:找不到score字段。
究其原因就是因为Student.cources在运行期被Django设置为与Cources相关的Manager而不是我们预期的与StudentCourse相关的Manager。
实现以上需求实际上也非常简单,只对代码稍加调整:
1<!-- lang: python ---> 2cources = Student.objects.get(id=1).studentcourse_set 3for c in cources: 4 print 'Course:%s Score:%s\n' % (c.name,c.score)
原因是,从StudentCourse角度,他是建立量到两个模型(Student,Course)的多对1关系,Django在启动时,会自动在其关联的模型上建立"[model]_set"的属性,就想常规的多对一关系一样——实际上他就是常规的多对一关系,只不过Student让其充当另外的角色罢了。