我有两个模型:League
和Team
. Team
有一个外键链接到League
. 我希望能够Team
根据存储在League
. 具体来说:
class League(models.Model):
number_of_teams = models.IntegerField()
class Team(models.Model):
league = models.ForeignKey(League)
draft_slot = models.IntegerField(choices=[(i+1,i+1) for i in range(?????????)])
我认识到我无法draft_slot.choices
在Team
模型中准确定义我的。所以我希望这样设置Team
:
class Team(models.Model):
league = models.ForeignKey(League)
draft_slot = models.IntegerField()
我已经设置了ModelForm
一个Team
:
class TeamModelForm(ModelForm):
class Meta:
model = Team
并查看Team
ModelForm
:
def SetupTeam(request, fanatic_slug=None, league_slug=None):
league = League.objects.get(slug=league_slug)
form = TeamModelForm()
return render_to_response('league/addteam.html', {
'form': form
}, context_instance = RequestContext(request))
我需要什么 foo 才能使用league.id
,league.number_of_teams
所以TeamModelForm
预填充视图team.league
并呈现一个字段来表示team.draft_slot
看起来像
draft_slot = models.IntegerField(choices=[(i+1,i+1) for i in range(league.number_of_teams+1)])