Fixed #24497 -- Added Widget.supports_microseconds attribute
This commit is contained in:
parent
6123e6134f
commit
4dcc649341
|
@ -620,7 +620,7 @@ class BoundField(object):
|
||||||
# If this is an auto-generated default date, nix the
|
# If this is an auto-generated default date, nix the
|
||||||
# microseconds for standardized handling. See #22502.
|
# microseconds for standardized handling. See #22502.
|
||||||
if (isinstance(data, (datetime.datetime, datetime.time)) and
|
if (isinstance(data, (datetime.datetime, datetime.time)) and
|
||||||
not getattr(self.field.widget, 'supports_microseconds', True)):
|
not self.field.widget.supports_microseconds):
|
||||||
data = data.replace(microsecond=0)
|
data = data.replace(microsecond=0)
|
||||||
self._initial_value = data
|
self._initial_value = data
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -180,6 +180,7 @@ class Widget(six.with_metaclass(MediaDefiningClass)):
|
||||||
needs_multipart_form = False # Determines does this widget need multipart form
|
needs_multipart_form = False # Determines does this widget need multipart form
|
||||||
is_localized = False
|
is_localized = False
|
||||||
is_required = False
|
is_required = False
|
||||||
|
supports_microseconds = True
|
||||||
|
|
||||||
def __init__(self, attrs=None):
|
def __init__(self, attrs=None):
|
||||||
if attrs is not None:
|
if attrs is not None:
|
||||||
|
|
|
@ -1455,15 +1455,27 @@ class FormsTestCase(TestCase):
|
||||||
def delayed_now_time():
|
def delayed_now_time():
|
||||||
return now.time()
|
return now.time()
|
||||||
|
|
||||||
|
class HiddenInputWithoutMicrosec(HiddenInput):
|
||||||
|
supports_microseconds = False
|
||||||
|
|
||||||
|
class TextInputWithoutMicrosec(TextInput):
|
||||||
|
supports_microseconds = False
|
||||||
|
|
||||||
class DateTimeForm(Form):
|
class DateTimeForm(Form):
|
||||||
auto_timestamp = DateTimeField(initial=delayed_now)
|
auto_timestamp = DateTimeField(initial=delayed_now)
|
||||||
auto_time_only = TimeField(initial=delayed_now_time)
|
auto_time_only = TimeField(initial=delayed_now_time)
|
||||||
supports_microseconds = DateTimeField(initial=delayed_now, widget=TextInput)
|
supports_microseconds = DateTimeField(initial=delayed_now, widget=TextInput)
|
||||||
|
hi_default_microsec = DateTimeField(initial=delayed_now, widget=HiddenInput)
|
||||||
|
hi_without_microsec = DateTimeField(initial=delayed_now, widget=HiddenInputWithoutMicrosec)
|
||||||
|
ti_without_microsec = DateTimeField(initial=delayed_now, widget=TextInputWithoutMicrosec)
|
||||||
|
|
||||||
unbound = DateTimeForm()
|
unbound = DateTimeForm()
|
||||||
self.assertEqual(unbound['auto_timestamp'].value(), now_no_ms)
|
self.assertEqual(unbound['auto_timestamp'].value(), now_no_ms)
|
||||||
self.assertEqual(unbound['auto_time_only'].value(), now_no_ms.time())
|
self.assertEqual(unbound['auto_time_only'].value(), now_no_ms.time())
|
||||||
self.assertEqual(unbound['supports_microseconds'].value(), now)
|
self.assertEqual(unbound['supports_microseconds'].value(), now)
|
||||||
|
self.assertEqual(unbound['hi_default_microsec'].value(), now)
|
||||||
|
self.assertEqual(unbound['hi_without_microsec'].value(), now_no_ms)
|
||||||
|
self.assertEqual(unbound['ti_without_microsec'].value(), now_no_ms)
|
||||||
|
|
||||||
def test_help_text(self):
|
def test_help_text(self):
|
||||||
# You can specify descriptive text for a field by using the 'help_text' argument)
|
# You can specify descriptive text for a field by using the 'help_text' argument)
|
||||||
|
|
Loading…
Reference in New Issue