用户只需填写两个字段中的一个 我怎么会要这个?它会在哪里?
我正在尝试将其保存在保存中,但我相信它属于其他地方,因为验证错误弹出为Django错误屏幕,而不是字段验证错误。这是我尝试过的:
class SignupForm(forms.Form):
learn1 = forms.CharField(max_length=50, label='Learn', required=False)
teach1 = forms.CharField(max_length=50, label='Teach', required=False)
class Meta:
model = Profile
def save(self, user):
if not self.cleaned_data['learn1'] or self.cleaned_data['teach1']:
raise forms.ValidationError("Specify at least one")
user.is_profile_to.learn1 = self.cleaned_data['learn1']
user.is_profile_to.teach1 = self.cleaned_data['teach1']
user.save()
user.is_profile_to.save()
答案 0 :(得分:3)
def clean(self):
cleaned_data = super().clean()
if not self.cleaned_data['learn1'] or self.cleaned_data['teach1']:
raise forms.ValidationError("Specify at least one")
else:
return cleaned_data
def save(self, user):
user.is_profile_to.learn1 = self.cleaned_data['learn1']
user.is_profile_to.teach1 = self.cleaned_data['teach1']
user.save()
user.is_profile_to.save()