diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2016-08-18 17:55:47 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-18 17:55:47 -0700 |
| commit | f5c6d3c8d9fa2158734858fa0a7ac917c384cb97 (patch) | |
| tree | 0f3846b3df6a78c674d3ef048235d5604d469186 /django/forms | |
| parent | 13857b45ca54a519a58361238442af84262c0d23 (diff) | |
Fixed #27068 -- Unified form field initial data retrieval.
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/boundfield.py | 23 | ||||
| -rw-r--r-- | django/forms/forms.py | 20 |
2 files changed, 24 insertions, 19 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index a9b4c5e063..e68744dbdc 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -129,12 +129,9 @@ class BoundField(object): Returns the value for this BoundField, using the initial value if the form is not bound or the data otherwise. """ - if not self.form.is_bound: - data = self.initial - else: - data = self.field.bound_data( - self.data, self.form.initial.get(self.name, self.field.initial) - ) + data = self.initial + if self.form.is_bound: + data = self.field.bound_data(self.data, data) return self.field.prepare_value(data) def label_tag(self, contents=None, attrs=None, label_suffix=None): @@ -218,14 +215,12 @@ class BoundField(object): @cached_property def initial(self): - data = self.form.initial.get(self.name, self.field.initial) - if callable(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) + 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 def build_widget_attrs(self, attrs, widget=None): diff --git a/django/forms/forms.py b/django/forms/forms.py index 25e081e324..17d4598f4c 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -377,12 +377,12 @@ class BaseForm(object): # Each widget type knows how to retrieve its own data, because some # widgets split data over several HTML fields. if field.disabled: - value = self.initial.get(name, field.initial) + value = self.get_initial_for_field(field, name) else: value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: if isinstance(field, FileField): - initial = self.initial.get(name, field.initial) + initial = self.get_initial_for_field(field, name) value = field.clean(value, initial) else: value = field.clean(value) @@ -431,9 +431,9 @@ class BaseForm(object): prefixed_name = self.add_prefix(name) data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) if not field.show_hidden_initial: - initial_value = self.initial.get(name, field.initial) - if callable(initial_value): - initial_value = initial_value() + # Use the BoundField's initial as this is the value passed to + # the widget. + initial_value = self[name].initial else: initial_prefixed_name = self.add_initial_prefix(name) hidden_widget = field.hidden_widget() @@ -482,6 +482,16 @@ class BaseForm(object): """ return [field for field in self if not field.is_hidden] + def get_initial_for_field(self, field, field_name): + """ + Return initial data for field on form. Use initial data from the form + or the field, in that order. Evaluate callable values. + """ + value = self.initial.get(field_name, field.initial) + if callable(value): + value = value() + return value + class Form(six.with_metaclass(DeclarativeFieldsMetaclass, BaseForm)): "A collection of Fields, plus their associated data." |
