Django循环模型参考

Django循环模型参考

本文介绍了Django循环模型参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在一个小型的足球联赛管理网站上工作(主要是为了学习目的),并且不能围绕着Django车型的关系。为了简单起见,假设我有两种类型的对象 - 玩家和团队。当然,一个球员属于一个球队,所以在球员模型中是一个ForeignKey(Team)。
所以我去:

  class Team(models.Model):
name = models.CharField )
class Player(models.Model):
name = models.CharField()
team = models.ForeignKey(Team)

然后,我希望每个队伍都有一个队长,这将是球队的一个球员,所以在Team模型中将是一个ForeignKey(Player)。但这将创造一个循环依赖。
授予我的Django经验是有限的,但它似乎是一个简单的问题,虽然我无法弄清楚我在概念上做错了什么。

解决方案

这是解决这个问题的另一种方法。
我创建了一个额外的表来存储玩家和团队之间的关系,而不是创建循环依赖。所以最终看起来像这样:

  class Team(Model):
name = CharField(max_length = 50 )

def get_captain(self):
返回PlayerRole.objects.get(team = self).player

类玩家(模型):
first_name = CharField(max_length = 50)
last_name = CharField(max_length = 50,blank = True)

def get_team(self):
返回PlayerRole.objects.get = b
PLAYER_ROLES =(
(Regular,Regular),
(Captain,Captain)


class PlayerRole(Model):
player = OneToOneField(Player,primary_key = True)
team = ForeignKey(Team,null = True)
role = CharField(max_length = 20,choices = PLAYER_ROLES,default = PLAYER_ROLES [0] [0])
class Meta:
unique_together =(player,team)
/ pre>

在存储方面的效率可能略低于建议的解决方法,但它避免了循环微小的依赖性,并保持数据库结构的清晰和清晰。
欢迎评论。


I'm starting to work on a small soccer league management website (mostly for learning purposes) and can't wrap my mind around a Django models relationship. For simplicity, let's say I have 2 types of objects - Player and Team. Naturally, a player belongs to one team so that's a ForeignKey(Team) in the Player model. So I go:

class Team(models.Model):
    name = models.CharField()
class Player(models.Model):
    name = models.CharField()
    team = models.ForeignKey(Team)

Then I want each team to have a captain which would be one of the players so that would be a ForeignKey(Player) in the Team model. But that would create a circular dependency.Granted my Django experience is limited, but it seems like a simple problem, though I can't figure out what I'm doing wrong conceptually.

解决方案

Here is another way to tackle this problem.Instead of creating a circular dependency, I created an additional table that stores the relationship between players and teams. So in the end it looks like this:

class Team(Model):
    name = CharField(max_length=50)

    def get_captain(self):
        return PlayerRole.objects.get(team=self).player

class Player(Model):
    first_name = CharField(max_length=50)
    last_name = CharField(max_length=50, blank=True)

    def get_team(self):
        return PlayerRole.objects.get(player=self).team

PLAYER_ROLES = (
    ("Regular", "Regular"),
    ("Captain", "Captain")
    )

class PlayerRole(Model):
    player = OneToOneField(Player, primary_key=True)
    team = ForeignKey(Team, null=True)
    role = CharField(max_length=20, choices=PLAYER_ROLES, default=PLAYER_ROLES[0][0])
    class Meta:
        unique_together = ("player", "team")

It might be slightly less efficient in terms of storage than the suggested workaround, but it avoids the circular dependency and keeps the DB structure clean and clear.Comments are welcome.

这篇关于Django循环模型参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:07