I have two models: League and Team. Team has a foreign key link to League. I want to be able to set choices available for the Team based on values stored in League. Specifically:
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(?????????)])
I recognize I cannot accurately define my draft_slot.choices in the Team model. So I would expect to set up Team like so:
class Team(models.Model):
league = models.ForeignKey(League)
draft_slot = models.IntegerField()
I have set up a ModelForm of Team:
class TeamModelForm(ModelForm):
class Meta:
model = Team
And a view of the 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))
What foo do I need in order to use league.id, league.number_of_teams so the view of TeamModelForm prepopulates team.league and also renders a field to represent team.draft_slot to look like
draft_slot = models.IntegerField(choices=[(i+1,i+1) for i in range(league.number_of_teams+1)])