30

I am creating a django application and I have the next problem: this error is shown when I want to set a date:

ValidationError [u"'12/06/2012' value has an invalid date format. It must be in YYYY-MM-DD format."]

For this model:

class ModelA(models.Model):

    date1 = models.DateField(null=True)
    date2 = models.DateField(null=True)

How can I set the DateField format to be %m/%d/%Y.

The option "input_formats" is not recognized.

Thank you!

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
jartymcfly
  • 1,945
  • 9
  • 30
  • 51

5 Answers5

30

input_formats is a forms.DateField option, not a model.DateField option. You have to set it in your form, not in your models.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
30

As @bruno as mentioned in his answer, input_formats is a forms field, however it can be used to control the date format saved from the model.

In settings.py set DATE_INPUT_FORMATS as below:

DATE_INPUT_FORMATS = ['%d-%m-%Y']

And in your form you could do something like below:

class ClientDetailsForm(ModelForm):
    date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
    class Meta:
       model = ModelA
tgogos
  • 23,218
  • 20
  • 96
  • 128
Vaulstein
  • 20,055
  • 8
  • 52
  • 73
2

You could also use the LANGUAGE_CODE to get the correct date formate for the local. LANGUAGE_CODE ='en-GB' Then have DATE_INPUT_FORMATS = ['%d-%m-%Y', '%Y-%m-%d'] in the settings.py which can be called when needed at anytime.

date_birth = forms.DateField(label='Date of birth', widget=forms.SelectDateWidget(years=YEAR_CHOICES, input_formats= DATE_INPUT_FORMATS))
Sahil
  • 1,387
  • 14
  • 41
Ibby
  • 69
  • 4
  • It seems like "input_formats" is no longer a valid kwarg. I've got this error: TypeError: SelectDateWidget.__init__() got an unexpected keyword argument 'input_formats' – 101is5 Jan 14 '22 at 10:49
  • I think this should be set as the correct answer. And forms.DateField(input_formats=['%d/%m/%Y']) is still valid. – Alex8752 Sep 30 '22 at 01:20
1

In settings.py, insert:

USE_L10N = False

DATE_INPUT_FORMATS = ['%d/%m/%Y']  
  • DATE_INPUT_FORMATS's data type must be a list, containing the format as a string.
  • %d/%m/%Y is just my format of choice.

As of lately(jan 2022), settings.py does not contain USE_L10N by default (neither DATE_INPUT_FORMATS, but this one is expected not the be there, I guess).
It seems like, although USE_L10N isn't there by default, not only it does exists somewhere but it's also set to True, because setting up DATE_INPUT_FORMATS alone (i.e. without making USE_L10N explicitly False) will not do the trick.

101is5
  • 309
  • 2
  • 12
0

For those of you who watch this in 2023!!! To make this work I just added:

DATE_INPUT_FORMATS = ["%d.%m.%Y"]
USE_L10N = False

in my settings.py and it worked. I now can input 20.12.2020 in my form. My form is auto generated from models.Model class

  • 2
    Isn't that exactly what @101is5 already answered? https://stackoverflow.com/a/70709867/13138364 – tdy Feb 03 '23 at 04:05