django模型字段限制从其他模型字段中选择

时间:2011-09-08 17:26:34

标签: python django django-models django-admin

我正在做一个足球应用程序,我有一个夹具或游戏模型,这样做有两个团队,而且它为游戏添加了时间和东西,但我也有FixtureRedCard和FixtureGoals,什么是现在发生的事情是FixtureGoals和FixtureRedCard有3个字段,固定装置的外键和一个打进目标的球队的外键然后另一个显示哪个球员做了...

基本夹具类

class Fixture(TitleAndSlugModel):
    """
    A division match fixture
    """
    division = models.ForeignKey(Division)
    fixture_date_time = models.DateTimeField()
    team_a = models.ForeignKey("team.Team", related_name="team_a")
    team_b = models.ForeignKey("team.Team", related_name="team_b")

FixtureGoal

class FixtureGoal(BaseModel):
    """
    A goal recorded against a match fixture
    """
    fixture = models.ForeignKey(Fixture)
    team = models.ForeignKey("team.Team")
    player = ChainedForeignKey(
      "team.TeamPlayer", 
      chained_field="team", 
      chained_model_field="team", 
      show_all=False,
      auto_choose=True,
      blank=True, null=True)

    class Meta:
        ordering = ["fixture", "team",]

    def __unicode__(self):
        return u'%s (%s)' % (self.fixture, self.player)

FixtureRedCard

class FixtureRedCard(BaseModel):
    """
    A red card recorded against a match fixture
    """
    fixture = models.ForeignKey(Fixture)
    team = models.ForeignKey("team.Team")
    player = ChainedForeignKey(
      "team.TeamPlayer", 
      chained_field="team", 
      chained_model_field="team", 
      show_all=False,
      auto_choose=True,
      blank=True, null=True)

    class Meta:
        ordering = ["fixture", "team",]

    def __unicode__(self):
        return u'%s (%s)' % (self.fixture, self.player)

我想要做的是将选择限制在Fixture上选择的team_a和team_b,对于fixtureredcard和fixturegoal类中的现场团队,我该如何实现这一目标?

谢谢

1 个答案:

答案 0 :(得分:0)

查看此Django Snippet以获取示例。