#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.33bcf07be4fed312963206ed50cbd122%40djangoproject.com.

Reply via email to