From 6b842c599865217529d835c0e20e881b98104295 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 12 Aug 2016 12:43:24 -0700 Subject: [PATCH] Made BoundField.initial use cached_property. Follow up to fab46ce6f5a0a58c4e5e39c9e5e412702beb4a64. --- django/forms/boundfield.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 1b5cbfc47c..a9b4c5e063 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -16,9 +16,6 @@ from django.utils.translation import ugettext_lazy as _ __all__ = ('BoundField',) -UNSET = object() - - @html_safe @python_2_unicode_compatible class BoundField(object): @@ -35,7 +32,6 @@ class BoundField(object): else: self.label = self.field.label self.help_text = field.help_text or '' - self._initial_value = UNSET def __str__(self): """Renders this field as an HTML widget.""" @@ -220,20 +216,16 @@ class BoundField(object): id_ = widget.attrs.get('id') or self.auto_id return widget.id_for_label(id_) - @property + @cached_property def initial(self): data = self.form.initial.get(self.name, self.field.initial) if callable(data): - if self._initial_value is not UNSET: - data = self._initial_value - else: - data = data() - # 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) - self._initial_value = data + data = data() + # 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 def build_widget_attrs(self, attrs, widget=None):