简体   繁体   English

Django-如何将外键选择限制为另一个模型中的ManyToMany字段

[英]Django - How to restrict Foreign Key choices to a ManyToMany field in another model

I have 3 models: Championship , Team and Match . 我有3个模型: ChampionshipTeamMatch Championship and Team are related with a ManyToManyField because each team can participate in multiple championships and each championship has many teams. ChampionshipTeamManyToManyField相关联,因为每个团队可以参加多个冠军,并且每个冠军都有很多团队。 And each match should be linked to a championship but also to 2 teams that are in the championship. 每场比赛都应与一个冠军联系在一起,但也应与冠军中的2个团队联系在一起。

class Championship(models.Model):
    name = models.CharField(max_length=100)
    teams = models.ManyToManyField(Team)

class Team(models.Model):
    name = models.CharField(max_length=100)

class Match(models.Model):
    championship = models.ForeignKey(Championship)
    team1 = models.ForeignKey(Team)
    team2 = models.ForeignKey(Team)
    score1 = models.PositiveIntegerField()
    score2 = models.PositiveIntegerField()

I would like to ensure that 'team1' and 'team2' are in 'championship'. 我想确保“ team1”和“ team2”处于“冠军”状态。 And also that 'team1' and 'team2' are different. 而且“ team1”和“ team2”是不同的。

How could I do that ? 我该怎么办?

Maybe i could use something like Django-smart-selects but i would prefer to avoid using a third-party app. 也许我可以使用Django-smart-selects之类的东西,但我宁愿避免使用第三方应用程序。

You can do model validation in the save method: 您可以在save方法中进行模型验证:

from django.core.exceptions import ValidationError


class Match(models.Model):
    championship = models.ForeignKey(Championship)

    team1 = models.ForeignKey(Team)
    team2 = models.ForeignKey(Team)

    score1 = models.PositiveIntegerField()
    score2 = models.PositiveIntegerField()

    def save(self, *args, **kwargs):
        if self.team1 == self.team2:
            raise ValidationError('The two teams in a match must be distinct')

        all_teams = self.championship.teams.all()

        if self.team1 not in all_teams or self.team2 not in all_teams:
            raise ValidationError('Both teams must be in the championship')

        return super(Match, self).save(*args, **kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将外键选择限制为同一模型中的另一个外键 - How to restrict Foreign Key choices to another Foreign Key in the same model Django-如何根据数据时间字段限制下拉菜单中的外键选择 - Django - How to restrict foreign key choices in dropdown menu depending on datatime field 限制外键 Django 的模型选择 - Limit model choices on foreign key Django Django管理员未显示外键字段的选择 - Django admin not showing choices for Foreign key field 无法理解Django的外键和manytomany字段 - could not understand foreign key and manytomany field in django Django 模型将外键设置为另一个模型的字段 - Django Model set foreign key to a field of another Model 基于同一模型中的另一个外键,动态限制Django模型中Foreignkey的选择 - Dynamically limit choices for Foreignkey in Django models based on another foreign key in the same model 如何在 Django Rest Framework 中为作为另一个模型上的外键的模型添加附加字段 - How to add additional field in Django Rest Framework for a model that is a foreign key on another model Django外键基于模型上的另一个字段自动填充 - Django Foreign Key automatically filled based off of another field on model 将多处字段更改为外键时,Django迁移错误 - Django migration error when changing manytomany field to Foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM