Opened 16 years ago

Closed 11 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: dev
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

Description

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 16 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by uggedal, 16 years ago

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]
            else:
                value = '%s %s' % tuple(value)

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

comment:2 by Jacob, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by antondubkov, 16 years ago

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 by Karen Tracey, 16 years ago

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 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:6 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 by Jelko Arnds, 12 years ago

Cc: j.arnds@… added

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

comment:9 by Jacob, 12 years ago

Triage Stage: Design decision neededAccepted

comment:10 by Claude Paroz, 11 years ago

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