From d3f5f219f5f42ac3504ed626dcb92f4ee2dc3d5f Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Sun, 28 Nov 2010 02:50:31 +0000 Subject: [PATCH] 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 --- django/forms/forms.py | 26 +++--- docs/ref/forms/api.txt | 97 ++++++++++++---------- tests/regressiontests/forms/tests/forms.py | 15 ++++ 3 files changed, 86 insertions(+), 52 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