0
我知道如何在我的django管理表單中的特定字段上引發錯誤,但我希望在字段集上引發錯誤。我目前有一個字段集中的複選框列表,並希望引發錯誤,而不是在特定字段(又名特定複選框)上,而是在整個字段集上。Django表單驗證,在字段集上引發錯誤
這裏是我的Django的admin.py
class EventAdmin(admin.ModelAdmin):
form = EventForm
fieldsets = [
(None, {'fields': [
'approval_state',
'title',
'description'
]
}
),
('Group Owner', {'fields': [
'grpOwner_vcoe',
'grpOwner_cssi',
'grpOwner_essc',
'grpOwner_tmscc',
'grpOwner_inmc',
'grpOwner_cc7',
'grpOwner_ias',
]
}
), ...
class EventForm(forms.ModelForm):
# Form validation
def clean(self):
# Collect data
start = self.cleaned_data.get('start')
end = self.cleaned_data.get('end')
grpOwner_vcoe = self.cleaned_data.get('grpOwner_vcoe')
grpOwner_cssi = self.cleaned_data.get('grpOwner_cssi')
grpOwner_essc = self.cleaned_data.get('grpOwner_essc')
grpOwner_tmscc = self.cleaned_data.get('grpOwner_tmscc')
grpOwner_inmc = self.cleaned_data.get('grpOwner_inmc')
grpOwner_cc7 = self.cleaned_data.get('grpOwner_cc7')
grpOwner_ias = self.cleaned_data.get('grpOwner_ias')
if not (grpOwner_vcoe or grpOwner_cssi or grpOwner_essc or grpOwner_tmscc or grpOwner_inmc or grpOwner_cc7 or grpOwner_ias):
if not self._errors.has_key('Group Owner'):
self._errors['Group Owner'] = ErrorList()
self._errors['Group Owner'].append('Test')
# Check start & end data
if start > end:
if not self._errors.has_key('start'):
self._errors['start'] = ErrorList()
self._errors['start'].append('Event start must occur before event end')
return self.cleaned_data
但這並不,我知道我可以提高它在每個領域,但我覺得它更優雅,如果我能做到這一點周圍的fielset