Opened 17 years ago
Closed 12 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)
Change History (11)
by , 17 years ago
| Attachment: | django.datetimefield.optional.time.component.diff added |
|---|
comment:1 by , 17 years ago
comment:2 by , 17 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|
comment:3 by , 17 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 , 17 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 , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
comment:8 by , 13 years ago
| Cc: | added |
|---|
Any possibilities that this gets solved/released soon? Whats the status on the issue?
comment:9 by , 13 years ago
| Triage Stage: | Design decision needed → Accepted |
|---|
comment:10 by , 12 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
We've deprecated usage of SplitDateTimeWidget with DateTimeField in 0179852d7faf461d55cf3ae69393abb3f3cd2910.
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)