[Django]-Django form doesn't get the value in clean method

10👍

You should use clean method instead of clean_<fieldname> to validate fields that depend on each other (Django clean() docs).

class UploadFileForm(form.Form):
    subject = forms.ModelChoiceField(queryset=Subject.objects.all(), required=False)
    subject1 = forms.CharField(required=False)

    def clean(self):
        subject = self.cleaned_data.get('subject')
        subject1 = self.cleaned_data.get('subject1')
        if not subject and not subject1:
            raise forms.ValidationError('Subject field is required.')
        return self.cleaned_data
👤niekas

Leave a comment