diff options
| author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2021-07-13 22:15:13 -0400 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-07-16 15:51:20 +0200 |
| commit | 0dc25526d8daaaa59968d4b1b597968e197f040c (patch) | |
| tree | bde70ecd2cf8000c08553274903ac42bad86bd00 /django/forms | |
| parent | 788441c6ab4e0167aadc155a4362a87694e38aa5 (diff) | |
Fixed #32924 -- Changed BaseForm.get_initial_for_field() to remove microseconds when needed.
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 |
