diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2007-01-21 05:14:50 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2007-01-21 05:14:50 +0000 |
| commit | 549985bcba3474f497114d5eec803edcd18b342f (patch) | |
| tree | b878903becc3986c6eb36998580db9fb1ae93532 | |
| parent | 9fb548fdcd7af8fdbfca233a4d6938a724521e36 (diff) | |
newforms-admin: Merged to [4379]
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4380 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/newforms/fields.py | 12 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 51 |
2 files changed, 62 insertions, 1 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 2370d964a5..3ba4f77c22 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -319,10 +319,20 @@ class BooleanField(Field): class ChoiceField(Field): def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None): if isinstance(widget, type): - widget = widget(choices=choices) + widget = widget() super(ChoiceField, self).__init__(required, widget, label, initial) self.choices = choices + def _get_choices(self): + return self._choices + + def _set_choices(self, value): + # Setting choices also sets the choices on the widget. + self._choices = value + self.widget.choices = value + + choices = property(_get_choices, _set_choices) + def clean(self, value): """ Validates that the input is in self.choices. diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 336edf6cb7..5f003711dc 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1830,6 +1830,57 @@ For a form with a <select>, use ChoiceField: <option value="J">Java</option> </select> +You can specify widget attributes in the Widget constructor. +>>> class FrameworkForm(Form): +... name = CharField() +... language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(attrs={'class': 'foo'})) +>>> f = FrameworkForm(auto_id=False) +>>> print f['language'] +<select class="foo" name="language"> +<option value="P">Python</option> +<option value="J">Java</option> +</select> +>>> f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False) +>>> print f['language'] +<select class="foo" name="language"> +<option value="P" selected="selected">Python</option> +<option value="J">Java</option> +</select> + +When passing a custom widget instance to ChoiceField, note that setting +'choices' on the widget is meaningless. The widget will use the choices +defined on the Field, not the ones defined on the Widget. +>>> class FrameworkForm(Form): +... name = CharField() +... language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(choices=[('R', 'Ruby'), ('P', 'Perl')], attrs={'class': 'foo'})) +>>> f = FrameworkForm(auto_id=False) +>>> print f['language'] +<select class="foo" name="language"> +<option value="P">Python</option> +<option value="J">Java</option> +</select> +>>> f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False) +>>> print f['language'] +<select class="foo" name="language"> +<option value="P" selected="selected">Python</option> +<option value="J">Java</option> +</select> + +You can set a ChoiceField's choices after the fact. +>>> class FrameworkForm(Form): +... name = CharField() +... language = ChoiceField() +>>> f = FrameworkForm(auto_id=False) +>>> print f['language'] +<select name="language"> +</select> +>>> f.fields['language'].choices = [('P', 'Python'), ('J', 'Java')] +>>> print f['language'] +<select name="language"> +<option value="P">Python</option> +<option value="J">Java</option> +</select> + Add widget=RadioSelect to use that widget with a ChoiceField. >>> class FrameworkForm(Form): ... name = CharField() |
