I’d like to display an alternative text (let’s say ‘any’) for no or empty value in a DateField’s TextInput widget.
My idea was to set the form field’s initial value to ‘any’ and then check in its clean_<field>() method for this initial value and cleaning it back to None:
date_field = forms.DateField(initial='any', required=False)
def clean_date_field(self):
data = self.cleaned_data['date_field']
if data == 'any':
data = None
return data
Unfortunately, the clean_<field>() method is never called, I think because some date format validation takes place in advance and raises a ValidationError which will prevent the clean_<field>() method from ever being called.
Is there a simple way to display an alternative form field value for None using a DateField?
Any hints are appreciated.
You can create custom field and override
to_pythonmethod.to_pythonwill be called before field’s clean, and you can convertanytoNonevalue there.Also you can use HTML5
placeholder.