Ticket #9721: django.datetimefield.optional.time.component.diff

File django.datetimefield.optional.time.component.diff, 1.5 KB (added by uggedal, 16 years ago)
  • django/forms/fields.py

     
    381381            # components: date and time.
    382382            if len(value) != 2:
    383383                raise ValidationError(self.error_messages['invalid'])
    384             value = '%s %s' % tuple(value)
     384            # To support input formats consisting of a date component only.
     385            if value[1] in EMPTY_VALUES:
     386                value = value[0]
     387            else:
     388                value = '%s %s' % tuple(value)
    385389        for format in self.input_formats:
    386390            try:
    387391                return datetime.datetime(*time.strptime(value, format)[:6])
  • tests/regressiontests/forms/fields.py

     
    601601...
    602602ValidationError: [u'Enter a valid date/time.']
    603603
     604DateTimeField supports list input:
     605>>> f.clean(['2008-11-22', '14:30'])
     606datetime.datetime(2008, 11, 22, 14, 30)
     607>>> f.clean(['2008-11-22', ''])
     608datetime.datetime(2008, 11, 22, 0, 0)
     609>>> f.clean(['2008-11-22'])
     610Traceback (most recent call last):
     611...
     612ValidationError: [u'Enter a valid date/time.']
     613
    604614DateField accepts an optional input_formats parameter:
    605615>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
    606616>>> f.clean(datetime.date(2006, 10, 25))
Back to Top