1
我正在尝试使用django制作一个web应用程序,该程序允许用户选择他们相信谁会在几场比赛中获胜的人。我将比赛作为一个模型,包括主队,客队,身份证等。现在提交时,我希望用户将多行插入到具有用户标识和所选团队的表中。Django表单列表中的每个项目
我已经尝试使用formset,但我无法弄清楚他们如何使用不同的标签。
我现在拥有的形式就是这个。
class PickForm(forms.ModelForm):
'''
A form that allows a user to make a pick on the
selected game
'''
error_messages = {
'no_match': ('Your selections do not match the corresponding options')
}
team_picked = forms.CharField(label=('Your choice'))
class Meta:
model = Pick
fields = ('team_picked',)
def __init__(self, *args, **kwargs):
self.user_id = kwargs.pop('user_id', None)
self.matchweek = kwargs.pop('matchweek', None)
super(PickForm, self).__init__(*args, **kwargs)
def clean_team_picked(self):
team_picked = self.cleaned_data['team_picked']
if(team_picked == self.home_team):
return team_picked
elif(team_picked == self.away_team):
return team_picked
else:
raise forms.ValidationError(
self.error_messages['no_match'],
code='no_match',
)
def save(self, commit=True):
pick = super(PickForm, self).save(commit=False)
pick.team_picked = self.cleaned_data['team_picked']
pick.user_id = self.user_id
pick.matchweek = self.matchweek
if commit:
pick.save()
return pick
谢谢!