1
0
mirror of https://github.com/django/django.git synced 2025-10-25 14:46:09 +00:00

Fixed #32924 -- Changed BaseForm.get_initial_for_field() to remove microseconds when needed.

This commit is contained in:
Chris Jerdonek
2021-07-13 22:15:13 -04:00
committed by Carlton Gibson
parent 788441c6ab
commit 0dc25526d8
3 changed files with 21 additions and 8 deletions

View File

@@ -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

View File

@@ -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