Django:多对多循环引用

时间:2017-06-19 22:01:39

标签: python django python-2.7 python-3.x django-models

我的模型中存在问题,因为我在多对多关系中有一个循环引用

这是我的网络应用程序的一部分,会话是一个小组(一个小组可以有一个以上的会话),每个会话都有多个助手,但助理只能是会议小组的成员

以下是我的模特:

class GroupMember(models.Model):
    member = models.ForeignKey(Volunteer, null=True, blank=True)
    group = models.ForeignKey(Group)
    leader = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{}'.format(self.member)

class Session(models.Model):
    name = models.CharField(max_length=250)
    group = models.ForeignKey(Group)
    assistants = models.ManyToManyField(GroupMember,through=SessionAssistant)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{}'.format(self.name)

class SessionAssistant(models.Model):
    assistant = models.ForeignKey(GroupMember)
    session = models.ForeignKey(Session, null=True, blank=True, on_delete=models.CASCADE)
    assist = models.BooleanField(default=True)
    ability = models.IntegerField(null=True,
                              blank=True,
                              validators=[MaxValueValidator(5), MinValueValidator(1)])
    performance = models.IntegerField(null=True,
                                  blank=True,
                                  validators=[MaxValueValidator(5), MinValueValidator(1)])
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{}'.format(self.assistant)

1 个答案:

答案 0 :(得分:0)

如果您将assistants = models.ManyToManyField(GroupMember,through=SessionAssistant)更改为assistants = models.ManyToManyField(GroupMember,through='SessionAssistant')会怎样?