中介模型

 1 class Student(models.Model):
 2     name = models.CharField( max_length=32)
 3     courses=models.ManyToManyField("Courses",through="Score")
 4     
 5 class Course(models.Model):
 6     name = models.CharField( max_length=32)    
 7                                     
 8 class Score(models.Model):
 9      student=models.ForeignKey("Student")
10      course=models.ForeignKey("Course")
11      score=models.IntegerField()

through直接指定第三张关联表,不再由django自行创建

 

像是这样自己创建的第三张表就属于中介模型,一般Django会给我们自动创建第三张表,人家自己创建的只是有关系字段,不能再增加其他的字段了,如果根据需求添加其他字段,不需要ManytoMany自己创建第三张表,只要我们再设计一张表,并用through关键字关联第三张表即可

posted @ 2018-11-23 16:41  阵浊秀  阅读(789)  评论(0)    收藏  举报