Opened 8 years ago

Closed 3 years ago

#9721 closed Bug (wontfix)

DateTimeField does not support all DEFAULT_DATETIME_INPUT_FORMATS when passed a list as input

Reported by: uggedal Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords: DateTimeFIeld SplitDateTimeWidget
Cc: j.arnds@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When using for instance a SplitDateTimeWidget with a DateTimeField one can not provide valid input to be parsed with the following formats:

    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y',              # '10/25/06'

One can therefore not create a split input solution where the time component is not required. The reason lies in SplitDateTimeWidget's clean method for list input. The date and time parts are concatenated together with a space between them. I included a patch for the simplest thing that would work (albeit possible not the best way to handle this).

SplitDateTimeField exists, but this field explicitly requires both date and time fields to be included.

Attachments (1)

django.datetimefield.optional.time.component.diff (1.5 KB) - added by uggedal 8 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 8 years ago by uggedal

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Until this issue have been resolved or rejected I simply made my own field which allows optional time components:

class DateOptionalTimeField(DateTimeField):
    DateTimeField where the time component is optional. To be used with
    SplitDateTimeWidget or a custom MutliWidget. Also see ticket #9721.

    def clean(self, value):
        if isinstance(value, list):
            # Input comes from a SplitDateTimeWidget, for example. So, it's two
            # components: date and time.
            if len(value) != 2:
                raise ValidationError(self.error_messages['invalid'])
            # To support input formats consisting of a date component only.
            if value[1] in EMPTY_VALUES:
                value = value[0]
                value = '%s %s' % tuple(value)

        return super(DateOptionalTimeField, self).clean(value)

comment:2 Changed 8 years ago by Jacob

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 8 years ago by antondubkov

I've encountered a problem with using DateTimeField(required=False) and SplitDateTimeWidget (or AdminSplitDateTime) - these widgets don't let you leave the field empty. This happens because in this case the value is [u, u], but EMPTY_VALUES = (None, ). So either EMPTY_VALUES should be extended, or value should be converted to string (if isinstance(value, list)) before checking against EMPTY_VALUES.

comment:4 Changed 8 years ago by Karen Tracey

Also related: #8898. In general it strikes me that using a SplitDateTimeWidget with a DateTimeField is a wrong approach. We've got the SplitDateTimeField to go with the SplitDateTimeWidget. If there is some use case that those two together don't support (half of it being optional) then I think that should be addressed by enhancing them, not trying to mix together a split widget with a non-split field.

comment:5 Changed 5 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:6 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 Changed 4 years ago by Jelko Arnds

Cc: j.arnds@… added

Any possibilities that this gets solved/released soon? Whats the status on the issue?

comment:9 Changed 4 years ago by Jacob

Triage Stage: Design decision neededAccepted

comment:10 Changed 3 years ago by Claude Paroz

Resolution: wontfix
Status: newclosed

We've deprecated usage of SplitDateTimeWidget with DateTimeField in 0179852d7faf461d55cf3ae69393abb3f3cd2910.

Note: See TracTickets for help on using tickets.
Back to Top