summaryrefslogtreecommitdiff
path: root/django/forms/boundfield.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2016-08-12 10:59:01 -0700
committerTim Graham <timograham@gmail.com>2016-08-12 13:59:01 -0400
commitfab46ce6f5a0a58c4e5e39c9e5e412702beb4a64 (patch)
tree361730d067142bfc0e51ecfd2e65b9d719f08a07 /django/forms/boundfield.py
parentf842d1011c1195aa26071a6ab6f96e0b8d907343 (diff)
Fixed #27037 -- Prevented required attribute on ClearableFileInput when initial data exists.
Diffstat (limited to 'django/forms/boundfield.py')
-rw-r--r--django/forms/boundfield.py31
1 files changed, 18 insertions, 13 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py
index e0ee6b4e78..1b5cbfc47c 100644
--- a/django/forms/boundfield.py
+++ b/django/forms/boundfield.py
@@ -134,18 +134,7 @@ class BoundField(object):
the form is not bound or the data otherwise.
"""
if not self.form.is_bound:
- 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 = self.initial
else:
data = self.field.bound_data(
self.data, self.form.initial.get(self.name, self.field.initial)
@@ -231,11 +220,27 @@ class BoundField(object):
id_ = widget.attrs.get('id') or self.auto_id
return widget.id_for_label(id_)
+ @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
+ return data
+
def build_widget_attrs(self, attrs, widget=None):
if not widget:
widget = self.field.widget
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
- if not widget.is_hidden and self.field.required and self.form.use_required_attribute:
+ if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
attrs['required'] = True
if self.field.disabled:
attrs['disabled'] = True