2014-09-05 51 views
-2
class RequestForm(forms.ModelForm): 
    """ Online request. 
    """ 
    class Meta: 
     model = Request 
     fields = ["language", "daytime", "daysofweek", "phone", "email", "name"] 
     widgets = { 
       'language': forms.Select(), 
       'daytime': RadioSelectNotNull(), 
       'daysofweek': forms.CheckboxSelectMultiple() 
     } 

如何安裝強制性填寫'phone'或'email'兩個字段之一?如何在django表單中設置不必要的字段?

回答

2

這不是完全清楚你問什麼,但如果目的是要求的phone至少一個或email非空白,使用自定義clean方法,這樣做:

def clean(self): 
    cleaned_data = super(ContactForm, self).clean() 
    if not (cleaned_data.get('phone') or cleaned_data.get('email')): 
     raise ValidationError('At least one of phone or email is required.') 
    return cleaned_data 
相關問題