Django ModelForm custom check: how to access the specified field values

I have a model below and want to add a special check in the "billable_work" field.

How do I access a field project that was submitted on the form? I want to check the project value ("p" in the example below), but cannot find the correct syntax so that I can check the presented value. Any help would be appreciated.

class EntryForm(forms.ModelForm): class Meta: model = Entries exclude = ('billable_work','notes') billable_work = forms.BooleanField() notes = forms.CharField(widget=forms.Textarea,required=False) def clean_billable_work(self): b = self.cleaned_data['billable_work'] p = form.fields['project'] if b == True and p == 523: raise forms.ValidationError(_("Entries cannot be both billable and NONE: Indirect.")) return self.cleaned_data['billable_work'] 
+7
source share
1 answer

I think you want to override the clean () method on your model, not the clean method of a specific form field. From the documents -

This method should be used to provide custom model validation and change the attributes on your model if necessary. For example, you could use it to automatically provide a value for a field or for which requires access to more than one field.

If you want to put validation on the form, then the clean() method on the form provides similar functionality (see docs ).

+12
source

All Articles