我正在尝试构建一个用于安排单个(或双重)淘汰锦标赛的表单。例如,考虑一个拥有TeamA,TeamB,TeamC和TeamD团队的联盟(所有这些团队都已在我的数据库中定义)。
表单应该类似于
Style: choice field - {single or double elimination}
Seed1: choice field - {TeamA or TeamB or TeamC or TeamD}
Seed2: choice field - {TeamA or TeamB or TeamC or TeamD}
Seed3: choice field - {TeamA or TeamB or TeamC or TeamD}
Seed4: choice field - {TeamA or TeamB or TeamC or TeamD}
这就是我的......
class EliminationForm(Form):
"""
Form for generating an elimination structure of Game instances
"""
choices = [(1, "Single Elimination"), (2, "Double Elimination")]
style = ChoiceField(choices=choices, widget=Select(attrs={'class':'form-control'}))
如何设置此表单以动态构建联盟中每个团队的“种子”字段?
这是我的models.py
class League(models.Model):
league_name = models.CharField(max_length=60)
class Team(models.Model):
league = models.ForeignKey('League')
team_name = models.CharField(max_length=60)
答案 0 :(得分:0)
我认为你要找的是ModelChoiceField。
它允许您使用数据库中的团队填充下拉列表:
class EliminationForm(Form):
"""
Form for generating an elimination structure of Game instances
"""
choices = [(1, "Single Elimination"), (2, "Double Elimination")]
style = ChoiceField(
choices=choices,
widget=Select(attrs={'class':'form-control'})
)
seed1 = ModelChoiceField(
queryset=Team.objects.all(),
)
seed2 = ModelChoiceField(
queryset=Team.objects.all(),
)
seed3 = ModelChoiceField(
queryset=Team.objects.all(),
)
seed4 = ModelChoiceField(
queryset=Team.objects.all(),
)
您还可以过滤所使用的对象,仅选择同一联赛的球队,或者只选择分配到联赛的球队等。
您可能还想在团队模型中添加__unicode__
功能,以定义它在下拉列表中的显示方式。
class Team(models.Model):
league = models.ForeignKey('League')
team_name = models.CharField(max_length=60)
def __unicode__(self):
return self.team_name