Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Fixed confusing behavior when setting choices for ChoiceFie…

…lds and their corresponding Widgets. Now, a Widget uses the choices from its ChoiceField regardless of whether the Widget has its own choices.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4378 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f073318668a40aab280a5e367e725e08538c52e3 1 parent c770803
Adrian Holovaty authored January 21, 2007
3  django/newforms/fields.py
@@ -319,7 +319,8 @@ def clean(self, value):
319 319
 class ChoiceField(Field):
320 320
     def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None):
321 321
         if isinstance(widget, type):
322  
-            widget = widget(choices=choices)
  322
+            widget = widget()
  323
+        widget.choices = choices
323 324
         super(ChoiceField, self).__init__(required, widget, label, initial)
324 325
         self.choices = choices
325 326
 
36  tests/regressiontests/forms/tests.py
@@ -1830,6 +1830,42 @@
1830 1830
 <option value="J">Java</option>
1831 1831
 </select>
1832 1832
 
  1833
+You can specify widget attributes in the Widget constructor.
  1834
+>>> class FrameworkForm(Form):
  1835
+...     name = CharField()
  1836
+...     language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(attrs={'class': 'foo'}))
  1837
+>>> f = FrameworkForm(auto_id=False)
  1838
+>>> print f['language']
  1839
+<select class="foo" name="language">
  1840
+<option value="P">Python</option>
  1841
+<option value="J">Java</option>
  1842
+</select>
  1843
+>>> f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
  1844
+>>> print f['language']
  1845
+<select class="foo" name="language">
  1846
+<option value="P" selected="selected">Python</option>
  1847
+<option value="J">Java</option>
  1848
+</select>
  1849
+
  1850
+When passing a custom widget instance to ChoiceField, note that setting
  1851
+'choices' on the widget is meaningless. The widget will use the choices
  1852
+defined on the Field, not the ones defined on the Widget.
  1853
+>>> class FrameworkForm(Form):
  1854
+...     name = CharField()
  1855
+...     language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(choices=[('R', 'Ruby'), ('P', 'Perl')], attrs={'class': 'foo'}))
  1856
+>>> f = FrameworkForm(auto_id=False)
  1857
+>>> print f['language']
  1858
+<select class="foo" name="language">
  1859
+<option value="P">Python</option>
  1860
+<option value="J">Java</option>
  1861
+</select>
  1862
+>>> f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
  1863
+>>> print f['language']
  1864
+<select class="foo" name="language">
  1865
+<option value="P" selected="selected">Python</option>
  1866
+<option value="J">Java</option>
  1867
+</select>
  1868
+
1833 1869
 Add widget=RadioSelect to use that widget with a ChoiceField.
1834 1870
 >>> class FrameworkForm(Form):
1835 1871
 ...     name = CharField()

0 notes on commit f073318

Please sign in to comment.
Something went wrong with that request. Please try again.