Fixed #32917 -- Added BaseForm._widget_data_value()/_field_data_value().

This commit is contained in:
Chris Jerdonek 2021-07-12 21:32:46 -07:00 committed by GitHub
parent bbf93efa19
commit 4be58fcc36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -118,7 +118,7 @@ class BoundField:
""" """
Return the data for this BoundField, or None if it wasn't given. Return the data for this BoundField, or None if it wasn't given.
""" """
return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) return self.form._field_data_value(self.field, self.html_name)
def value(self): def value(self):
""" """

View File

@ -191,6 +191,15 @@ class BaseForm:
"""Add an 'initial' prefix for checking dynamic initial values.""" """Add an 'initial' prefix for checking dynamic initial values."""
return 'initial-%s' % self.add_prefix(field_name) return 'initial-%s' % self.add_prefix(field_name)
def _widget_data_value(self, widget, html_name):
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
return widget.value_from_datadict(self.data, self.files, html_name)
def _field_data_value(self, field, html_name):
return self._widget_data_value(field.widget, html_name)
def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
"Output HTML. Used by as_table(), as_ul(), as_p()." "Output HTML. Used by as_table(), as_ul(), as_p()."
# Errors that should be displayed above all fields. # Errors that should be displayed above all fields.
@ -379,13 +388,10 @@ class BaseForm:
def _clean_fields(self): def _clean_fields(self):
for name, field in self.fields.items(): for name, field in self.fields.items():
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
if field.disabled: if field.disabled:
value = self.get_initial_for_field(field, name) value = self.get_initial_for_field(field, name)
else: else:
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) value = self._field_data_value(field, self.add_prefix(name))
try: try:
if isinstance(field, FileField): if isinstance(field, FileField):
initial = self.get_initial_for_field(field, name) initial = self.get_initial_for_field(field, name)
@ -432,8 +438,7 @@ class BaseForm:
def changed_data(self): def changed_data(self):
data = [] data = []
for name, field in self.fields.items(): for name, field in self.fields.items():
prefixed_name = self.add_prefix(name) data_value = self._field_data_value(field, self.add_prefix(name))
data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
if not field.show_hidden_initial: if not field.show_hidden_initial:
# Use the BoundField's initial as this is the value passed to # Use the BoundField's initial as this is the value passed to
# the widget. # the widget.
@ -442,8 +447,9 @@ class BaseForm:
initial_prefixed_name = self.add_initial_prefix(name) initial_prefixed_name = self.add_initial_prefix(name)
hidden_widget = field.hidden_widget() hidden_widget = field.hidden_widget()
try: try:
initial_value = field.to_python(hidden_widget.value_from_datadict( initial_value = field.to_python(
self.data, self.files, initial_prefixed_name)) self._widget_data_value(hidden_widget, initial_prefixed_name)
)
except ValidationError: except ValidationError:
# Always assume data has changed if validation fails. # Always assume data has changed if validation fails.
data.append(name) data.append(name)