这是解决此问题的另一种方法。我没有创建循环依赖关系,而是创建了一个额外的表来存储球员和球队之间的关系。所以最终看起来像这样:
class Team(Model): name = CharField(max_length=50) def get_captain(self): return PlayerRole.objects.get(team=self).playerclass 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).teamPLAYER_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")
在存储方面,它的效率可能比建议的解决方法略低,但是它避免了循环依赖,并保持了数据库结构的整洁。欢迎发表评论。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)