2016-05-17 32 views

回答

4

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 
相关问题