1

My settings file has these settings:

# settings.py
USE_L10N = False
DATE_INPUT_FORMATS = ['%m/%d/%Y']

In my test file, I'm creating an object requires a date and looks like this:

# tests.py
my_model = Thing(a_date='11/22/2019').save()

When I run the test, however, the test gets stuck when it goes to create the object and throws the error:

django.core.exceptions.ValidationError: ["'11/22/2019' value has an invalid date format. It must be in YYYY-MM-DD format."]

Is there something I'm missing? Why would it be throwing this error?

John R Perry
  • 3,916
  • 2
  • 38
  • 62

5 Answers5

1

You need to set DATE_INPUT_FORMATS, as DATE_FORMAT sets how Django displays the date.

Change your code with:

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

engin_ipek
  • 887
  • 1
  • 8
  • 10
1

As far as I know the DATE_INPUT_FORMATS is relevant for Forms but not for Models.

An (invalid) ticket regarding the same problem was raised here.

Chris
  • 2,162
  • 1
  • 6
  • 17
  • Well, not on the model level but on the form level. So e.g. if a user enters the date in one of the defined formats in a ```ModelForm``` and the form is saved, all is fine. If you try to create a model directly in you program using those formats it will not work. Let me put it this way: DATE_INPUT_FORMATS is for user interaction – Chris Jan 18 '20 at 08:35
0

Make sure that USE_L10N is set to FALSE.

(https://docs.djangoproject.com/en/dev/ref/settings/#date-format)

Also see: How to change the default Django date template format?

  • I added `DATE_INPUT_FORMATS = ['%m/%d/%Y']` and I'm still getting the error. – John R Perry Jan 17 '20 at 19:35
  • Are you sure the settings file is being found and applied? (e.g., https://stackoverflow.com/questions/42927394/django-settings-not-working-correctly) But Chris may have a point with the `Forms` but not `Models` issue (https://code.djangoproject.com/ticket/27364). – Josiah Bryan Jan 17 '20 at 21:24
0

maybe you need ad date_input_format read there and there

0

I ran into the same error while testing one of my views.

# settings.py
DATE_FORMAT = '%d.%m.%Y'
DATE_INPUT_FORMATS = ['%d.%m.%Y', '%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y', '%d %b %Y',
                      '%d %b, %Y', '%d %b %Y', '%d %b, %Y', '%d %B, %Y',
                      '%d %B %Y']

# forms.py
last_examination = forms.DateField(widget=forms.DateInput(
    attrs={
        'value': False,
        'class': 'datepicker',
        'data-date-format': 'dd.mm.yyyy',
        'data-date-autoclose': 'True',
    }
))

For a test I setup this data:

# test_views.py
cls.data2 = {'1-last_examination': date(day=19, month=2, year=1972),
                 '1-last_examination_result': 'OK'}

def test_Input2View_POST(self):
    self.c = Client()
    response = self.c.post(url, data=self.data2)

which raises a ValidationError Enter a valid date. Therefore the settings.py don't apply for this test, which was also stated by Chris comment.

Quick fix for this is hardcoding the correct date format, e.g. in my case

# test_views.py
cls.data2 = {'1-last_examination': '19.02.1972',
                 '1-last_examination_result': 'OK'}
NicoS
  • 31
  • 5