Python 根据同一型号中的选择筛选管理表单中的ForeignKey选择多个字段?

Python 根据同一型号中的选择筛选管理表单中的ForeignKey选择多个字段?,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我正在制作一个“匹配”模型,其中当前包含以下字段: event = models.ForeignKey(Event, on_delete=models.RESTRICT) match_participants = models.ManyToManyField(Wrestler, on_delete=models.RESTRICT, null=True, blank=True,) match_type = models.CharField(max_length=25, choices=match_

我正在制作一个“匹配”模型,其中当前包含以下字段:

event = models.ForeignKey(Event, on_delete=models.RESTRICT)
match_participants = models.ManyToManyField(Wrestler, on_delete=models.RESTRICT, null=True, blank=True,)
match_type = models.CharField(max_length=25, choices=match_choices, default=BLOCK)
match_result = models.CharField(max_length=25, choices=result_choices, default=NOT_OC)
winner = models.ForeignKey(Wrestler, on_delete=models.RESTRICT, null=True, blank=True, related_name="winner")
在我的场景中,“获胜者”只能是在“match_参与者”中选择的外键之一,我真的想过滤“获胜者”字段,只过滤那些选择的外键。这可能吗?我知道您可以根据其他模型中的条目筛选外键选择,但对同一个表条目不太确定

下面是我的数据库结构当前的UML示例:


您可以在多人关系中添加一个额外字段

models.py

class Match(models.Model):
    wrestlers = models.ManyToManyField(Wrestler, through='Participation' )
    ...



class Participation(models.Model):
    wrestler = models.ForeignKey(Wrestler, on_delete=models.CASCADE)
    match = models.ForeignKey(Match, on_delete=models.CASCADE)
    winner = models.BooleanField()
例如,为了进行验证,您可以使用信号连接到参与更新

def match_wrestlers_changed(sender, *args, **kwargs):
    if invalid:
        raise ValidationError('There already a winner.')

m2m_changed.connect(
    match_wrestlers_changed,
    sender=Match.wrestlers.through)

谢谢你,这对我来说是完美的。