I have this model:
class IeltsExam(Model):
student = OneToOneField(Student, on_delete=CASCADE)
has_taken_exam = BooleanField(default=False,)
listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
exam_date = DateField(null=True, blank=True, )
non_empty_fields = \
{
'listening': 'please enter your listening score',
'reading': 'please enter your reading score',
'exam_date': 'please specify your exam date',
}
def clean(self):
errors = {}
if self.has_taken_exam:
for field_name, field_error in self.non_empty_fields.items():
if getattr(self, field_name) is None:
errors[field_name] = field_error
if errors:
raise ValidationError(errors)
and have this modelform
class IeltsExamForm(ModelForm):
class Meta:
model = IeltsExam
fields = ('has_taken_exam', 'listening', 'reading', )
when I submit this form in template, I get the below error:
ValueError at /
'ExamForm' has no field named 'exam_date'.
and
During handling of the above exception ({'listening': ['please enter your listening score'], 'reading': ['please enter your reading score'], 'exam_date': ['please specify your exam date']}), another exception occurred:
The error happens in my view where I am validating the form. My database logic is such that I need to have an exam_date field and it should be mandatory to fill if has_taken_exam is checked. However, in ExamForm, for business reasons, I do not need the exam_date. How can I tell ExamForm to turn a blind eye to the exam_date, as I am not saving the modelform instance?