django Enter a valid date. validation error
I prefer to set **DATE_INPUT_FORMATS in settings.py and then define the field like:
dob = forms.DateField(input_formats=settings.DATE_INPUT_FORMATS)
This more DRY than setting it on a per form field basis.
If it's a text field input, I almost always put the accepted format(s) in the field's help_text
so that the user can know what format(s) is(are) accepted.
input_formats
needs to be a list, see
example:
['%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y'] # '10/25/06'
https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DateField
You do not need to add a new field once you already have a DateField
in your Model.
You just have to set the input_formats
for this field:
self.fields[ 'dob' ].input_formats = [ '%Y-%m-%d' ]
References: DateField and Format Localization