2
class FooForm(forms.ModelForm):
foo_date = forms.CharField(required=True)
foo_time = forms.CharField(required=True)
後,我如何能實現這樣的事情不是必需的。可能嗎?一兩個領域應填寫其中的一個
class FooForm(forms.ModelForm):
foo_date = forms.CharField(required=True)
foo_time = forms.CharField(required=True)
後,我如何能實現這樣的事情不是必需的。可能嗎?一兩個領域應填寫其中的一個
集required=False
爲兩個字段,然後檢查在clean
方法的至少一個被填充。
class FooForm(forms.ModelForm):
foo_date = forms.CharField(required=False)
foo_time = forms.CharField(required=False)
def clean(self):
cleaned_data = super(FooForm, self).clean()
if not (cleaned_data.get('foo_date') or cleaned_data.get('foo_time')):
raise forms.ValidationError('Either foo_date or foo_time is required')
return cleaned_data