[Django] #33066: DateTimeField does not exclusively validate the date time format against DATETIME_INPUT_FORMATS

10 views
Skip to first unread message

Django

unread,
Aug 28, 2021, 6:56:32 AM8/28/21
to django-...@googlegroups.com
#33066: DateTimeField does not exclusively validate the date time format against
DATETIME_INPUT_FORMATS
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Reksten-Monsen |
Type: Bug | Status: new
Component: Forms | Version: 3.2
Severity: Normal | Keywords: datetime iso8601
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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")
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33066>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 29, 2021, 6:36:18 AM8/29/21
to django-...@googlegroups.com
#33066: DateTimeField does not exclusively validate the date time format against
DATETIME_INPUT_FORMATS
-------------------------------------+-------------------------------------
Reporter: Christian Reksten- | Owner: nobody

Monsen |
Type: Bug | Status: new
Component: Forms | Version: 3.2
Severity: Normal | Resolution:

Keywords: datetime iso8601 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Christian Reksten-Monsen):

Reading the
[https://docs.djangoproject.com/en/3.2/ref/forms/fields/#datetimefield
docs on form DateTimeField], its seems that the intended functionality is
for the field to always accept ISO formats.

The field always accepts strings in ISO 8601 formatted dates or
similar recognized by parse_datetime().

I therefore suggest to add clarification on this to the
[https://docs.djangoproject.com/en/3.2/ref/settings/#datetime-input-
formats DATETIME_INPUT_FORMAT docs]. Maybe just simply adding below text
to the **Changed in Django 3.1** callout:

Support for ISO 8601 date string parsing (including optional
timezone) was added to DateTimeField form fields. The field always accepts
strings in ISO 8601 formatted dates or similar recognized by
parse_datetime(). The field will fallback on DATETIME_INPUT_FORMATS.

I have modified this ticket to reflect this being a documentation cleanup.

--
Ticket URL: <https://code.djangoproject.com/ticket/33066#comment:1>

Django

unread,
Aug 29, 2021, 6:36:47 AM8/29/21
to django-...@googlegroups.com
#33066: DateTimeField does not exclusively validate the date time format against
DATETIME_INPUT_FORMATS
-------------------------------------+-------------------------------------
Reporter: Christian Reksten- | Owner: nobody
Monsen |
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:

Keywords: datetime iso8601 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Christian Reksten-Monsen):

* type: Bug => Cleanup/optimization
* component: Forms => Documentation


--
Ticket URL: <https://code.djangoproject.com/ticket/33066#comment:2>

Django

unread,
Aug 30, 2021, 12:17:30 AM8/30/21
to django-...@googlegroups.com
#33066: Add annotation about accepting ISO 8601 formats by DateTimeField() to the
DATETIME_INPUT_FORMATS docs.

-------------------------------------+-------------------------------------
Reporter: Christian Reksten- | Owner: nobody
Monsen |
Type: | Status: closed

Cleanup/optimization |
Component: Documentation | Version: 3.2
Severity: Normal | Resolution: wontfix

Keywords: datetime iso8601 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Claude Paroz (added)
* status: new => closed
* resolution: => wontfix


Comment:

Thanks for the ticket, however this change is related with `DateTimeField`
not with the `DATETIME_INPUT_FORMATS` setting itself, its docs are still
valid. We normally don't include such `versionchanged` annotations and I
don't think this case is somehow special.

--
Ticket URL: <https://code.djangoproject.com/ticket/33066#comment:3>

Reply all
Reply to author
Forward
0 new messages