diff options
| author | Chris Beaven <smileychris@gmail.com> | 2010-11-28 02:50:31 +0000 |
|---|---|---|
| committer | Chris Beaven <smileychris@gmail.com> | 2010-11-28 02:50:31 +0000 |
| commit | d3f5f219f5f42ac3504ed626dcb92f4ee2dc3d5f (patch) | |
| tree | 610a47472e403afeeaceaa635b2108d883ee2cc2 /django/forms | |
| parent | e74edb4d53b089ec57ec4830eeba98607283a092 (diff) | |
Fixes #10427 -- Abstract the value generation of a BoundField
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14734 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/forms.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/django/forms/forms.py b/django/forms/forms.py index 0377de4767..1cf6f2a3a2 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -432,20 +432,11 @@ class BoundField(StrAndUnicode): else: attrs['id'] = self.html_initial_id - if not self.form.is_bound: - data = self.form.initial.get(self.name, self.field.initial) - if callable(data): - data = data() - else: - data = self.field.bound_data( - self.data, self.form.initial.get(self.name, self.field.initial)) - data = self.field.prepare_value(data) - if not only_initial: name = self.html_name else: name = self.html_initial_name - return widget.render(name, data, attrs=attrs) + return widget.render(name, self.value(), attrs=attrs) def as_text(self, attrs=None, **kwargs): """ @@ -470,6 +461,21 @@ class BoundField(StrAndUnicode): return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) data = property(_data) + def value(self): + """ + 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.form.initial.get(self.name, self.field.initial) + if callable(data): + data = data() + else: + data = self.field.bound_data( + self.data, self.form.initial.get(self.name, self.field.initial) + ) + return self.field.prepare_value(data) + def label_tag(self, contents=None, attrs=None): """ Wraps the given contents in a <label>, if the field has an ID attribute. |
