Django:限制OnetoMany关系中的关系数量

发布于 2024-10-10 10:31:14 字数 455 浏览 0 评论 0原文

您好,我希望将外键关系限制为特定数字。
假设一支篮球队只能有 12 个人。

class Team(models.Model):
 teamName = models.CharField(max_length = 20)
 teamColors = models.CharField(max_length = 20)
 ... <and so forth>

class Player(models.Models):
 team = models.ForeignKey(Team, **)
 name = models.CharField(max_length = 20)
 heightInches = models.IntegerField()
 ... <and so forth>

** 是否可以选择限制每队最多 12 名球员?

还有任何额外的创建 python 错误吗?

Hi i was looking to limit a ForeignKey relationship to as specific number.
Lets say there can only be 12 people on a basketball team.

class Team(models.Model):
 teamName = models.CharField(max_length = 20)
 teamColors = models.CharField(max_length = 20)
 ... <and so forth>

class Player(models.Models):
 team = models.ForeignKey(Team, **)
 name = models.CharField(max_length = 20)
 heightInches = models.IntegerField()
 ... <and so forth>

** is there an option to would limit up to 12 players per team here?

with any additional creating a python error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂ガ小子 2024-10-17 10:31:14

根据ForeignKey 定义,没有直接的方法来限制团队中的玩家数量。但是,这可以通过对模型进行一些操作来完成。

一种选择是在 Team 上创建一个方法,例如:

def add_player(self, player):
    if self.player_set.count() >= 12:
         raise Exception("Too many players on this team")

    self.player_set.add(player)

然后您希望始终通过此方法添加玩家。

There isn't a direct way to limit the number of players in a team on the ForeignKey definition. However, this can be done with a little bit of working with your model.

One option would be to make a method on Team, something like:

def add_player(self, player):
    if self.player_set.count() >= 12:
         raise Exception("Too many players on this team")

    self.player_set.add(player)

Then you would want to always add players through this method.

沦落红尘 2024-10-17 10:31:14

我认为另一种选择是重写 Player 模型上的保存方法,如下所示:

class Player(models.Models):
    team = models.ForeignKey(Team, **)
    name = models.CharField(max_length = 20)
    heightInches = models.IntegerField()
    ... <and so forth>

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if self.player_set.count() < 12:
            super(Player, self).save()
        else:
            raise Exception(f'{self.team.teamName} has already 12 players. No more are allowed.')

I think another option would be to override the save method on the Player model like this:

class Player(models.Models):
    team = models.ForeignKey(Team, **)
    name = models.CharField(max_length = 20)
    heightInches = models.IntegerField()
    ... <and so forth>

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if self.player_set.count() < 12:
            super(Player, self).save()
        else:
            raise Exception(f'{self.team.teamName} has already 12 players. No more are allowed.')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文