﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33066	Add annotation about accepting ISO 8601 formats by DateTimeField() to the DATETIME_INPUT_FORMATS docs.	Christian Reksten-Monsen	nobody	"According to the documentation on DATETIME_INPUT_FORMATS, the given list of date time formats will be accepted when inputting data on a datetime field. My assumption is that Django will ONLY accept formats from this list (and also DATE_INPUT_FORMATS, as mentioned in the docs). Django will, however, also accept ISO 8601 date inputs, even though these formats are not present in DATETIME_INPUT_FORMATS. This is not mentioned in the docs.

This is because of [https://github.com/django/django/pull/11893 PR #11893] for [https://code.djangoproject.com/ticket/11385 ticket 11385] which added support for ISO 8601 date inputs. ISO 8601 matching is done in DateTimeField and if there is a match, the BaseTemporalField to_python method (which checks date time format against DATETIME_INPUT_FORMATS) is never called. 

This was confusing for me when unit testing a form field, a validation error was not raised when giving a date format that was not present in DATETIME_INPUT_FORMATS. Below test case replicates the issue.

Possible solutions:
* Add the date time format (with timezone format, which I believe was the issue) to DATETIME_INPUT_FORMATS default. Python 2.7 and 3 strptime() do support %z.
* Ignore it and update the docs that ISO 8601 date time formats will be accepted, regardless of not being present in DATETIME_INPUT_FORMATS.
* Set a flag to strictly adhere DATETIME_INPUT_FORMATS or also allow ISO 8601 date time formats.

{{{#!python
from datetime import datetime
from django.core.exceptions import ValidationError
from <app_name>.settings import DATETIME_INPUT_FORMATS

from django.test import TestCase
class TestDateTimeField(TestCase):
    def test_field_validation(self):
        with self.settings(DATETIME_INPUT_FORMATS=['%d/%m/%Y %H:%M']):
            field = DateTimeField(required=True)
            self.assertIsInstance(field.clean(""31/12/2021 14:30""), datetime)
            with self.assertRaises(ValidationError):
                field.clean(""2021-12-31 14:30"")
}}}"	Cleanup/optimization	closed	Documentation	3.2	Normal	wontfix	datetime iso8601	Claude Paroz	Unreviewed	0	0	0	0	0	0
