简体   繁体   中英

django model field limit choice from other model fields

Well Im doing a football application and I have a fixture or game model, what this does is gets two teams, and well it adds a time and stuff for the game to happen, but I also have FixtureRedCard and FixtureGoals, what is happening now is that FixtureGoals and FixtureRedCard have 3 fields, the foreign key to fixtures and a foreign key to a team that scored the goal an then another to show which player did it...

Basic Fixture Class

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)

What I want to do is limit the choices to team_a and team_b chosen on Fixture, for field team in fixtureredcard and fixturegoal classes, how can I achieve this?

Thank you

请查看此Django代码段中的示例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM