diff options
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/boundfield.py | 9 | ||||
| -rw-r--r-- | django/forms/forms.py | 6 |
2 files changed, 7 insertions, 8 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 54f9e9a64f..2ff8b0ee26 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -1,4 +1,3 @@ -import datetime import re from django.core.exceptions import ValidationError @@ -228,13 +227,7 @@ class BoundField: @cached_property def initial(self): - data = self.form.get_initial_for_field(self.field, self.name) - # If this is an auto-generated default date, nix the microseconds for - # standardized handling. See #22502. - if (isinstance(data, (datetime.datetime, datetime.time)) and - not self.field.widget.supports_microseconds): - data = data.replace(microsecond=0) - return data + return self.form.get_initial_for_field(self.field, self.name) def build_widget_attrs(self, attrs, widget=None): widget = widget or self.field.widget diff --git a/django/forms/forms.py b/django/forms/forms.py index ac6ef667d9..2bf268ae76 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -3,6 +3,7 @@ Form classes """ import copy +import datetime from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.forms.fields import Field, FileField @@ -475,6 +476,11 @@ class BaseForm: value = self.initial.get(field_name, field.initial) if callable(value): value = value() + # If this is an auto-generated default date, nix the microseconds + # for standardized handling. See #22502. + if (isinstance(value, (datetime.datetime, datetime.time)) and + not field.widget.supports_microseconds): + value = value.replace(microsecond=0) return value |
