1
私はdjangoを使ってWebアプリケーションを作成しようとしています。これは、ユーザーがいくつかのゲームで勝者になると信じる人を選ぶことを可能にします。私はホームチーム、離れたチーム、IDなどを持つモデルとしてゲームを持っています。今すぐ提出すると、ユーザーIDと選択したチームを持つテーブルに複数の行を挿入するようにしたいと思います。リスト内の各アイテムのDjangoフォーム
私は書式設定を使用しようとしましたが、別のラベルでどのように動作するかわかりません。
私が現在持っているフォームはこれです。
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
ありがとうございます!