Ticket #13205: django-empty-datetimefield.patch

File django-empty-datetimefield.patch, 2.0 KB (added by Bastian Kleineidam <calvin@…>, 15 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 8d4fbc4..71129c8 100644
    a b class DateTimeField(Field):  
    376376        Python datetime.datetime object.
    377377        """
    378378        super(DateTimeField, self).clean(value)
    379         if value in EMPTY_VALUES:
    380             return None
    381         if isinstance(value, datetime.datetime):
    382             return value
    383         if isinstance(value, datetime.date):
    384             return datetime.datetime(value.year, value.month, value.day)
    385379        if isinstance(value, list):
    386380            # Input comes from a SplitDateTimeWidget, for example. So, it's two
    387381            # components: date and time.
    388382            if len(value) != 2:
    389383                raise ValidationError(self.error_messages['invalid'])
     384            if value[0] in EMPTY_VALUES and value[1] in EMPTY_VALUES:
     385                return None
    390386            value = '%s %s' % tuple(value)
     387        elif value in EMPTY_VALUES:
     388            return None
     389        if isinstance(value, datetime.datetime):
     390            return value
     391        if isinstance(value, datetime.date):
     392            return datetime.datetime(value.year, value.month, value.day)
    391393        for format in self.input_formats:
    392394            try:
    393395                return datetime.datetime(*time.strptime(value, format)[:6])
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index 48da0f9..be091e0 100644
    a b ValidationError: [u'Enter a valid date/time.']  
    640640>>> f.clean('')
    641641>>> repr(f.clean(''))
    642642'None'
     643>>> f.clean(['', ''])
     644>>> repr(f.clean(['', '']))
     645'None'
     646>>> f.clean([None, None])
     647>>> repr(f.clean([None, None]))
     648'None'
     649>>> f.clean(['', None])
     650>>> repr(f.clean(['', None]))
     651'None'
     652>>> f.clean([None, ''])
     653>>> repr(f.clean([None, '']))
     654'None'
    643655
    644656# RegexField ##################################################################
    645657
Back to Top