From 0dc25526d8daaaa59968d4b1b597968e197f040c Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Tue, 13 Jul 2021 22:15:13 -0400 Subject: Fixed #32924 -- Changed BaseForm.get_initial_for_field() to remove microseconds when needed. --- django/forms/boundfield.py | 9 +-------- django/forms/forms.py | 6 ++++++ 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'django/forms') 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 -- cgit v1.3