我想知道如何检查表单集的有效性。
让我解释一下我的情况。我有五个包含在表单集中的表单。每个表格代表一组排球比赛(一场排球比赛分为5组)。
如果已经有代表胜利的三种形式,我希望能够提交我的表单,因为例如,如果我有一个小组赢得的前三局,第四局和第五局毫无用处,那么比赛结束了是三盘胜利。
我不清楚我是否很清楚...
您会在下面找到我的 forms.py
:
forms.py
class SetUpdateForm(forms.ModelForm):
class Meta:
model = Set
fields = [
'scoreTeam1',
'scoreTeam2',
'match',
]
def clean(self):
cleaned_data = super().clean()
scoreTeam1 = cleaned_data.get("scoreTeam1")
scoreTeam2 = cleaned_data.get("scoreTeam2")
if cleaned_data.get("match") is not None:
sport = cleaned_data.get("match").phase.tournament.sport
if scoreTeam1 and scoreTeam2:
if scoreTeam1 == scoreTeam2:
raise forms.ValidationError("Scores can't be equal")
if scoreTeam1 > sport.nbPointPerSet or scoreTeam2 > sport.nbPointPerSet or (scoreTeam1 < sport.nbPointPerSet and scoreTeam2 < sport.nbPointPerSet):
raise forms.ValidationError("A set is played in " + str(sport.nbPointPerSet) + "points.")
return cleaned_data
MatchSetFormset = forms.inlineformset_factory(Match, Set, form=SetUpdateForm, min_num=1, extra=0, can_delete=False)
我正在使用Django 2.2和Python 3.7.3