Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

newforms: Implemented RadioFieldRenderer.__getitem__(), which allows …

…for index lookup on radio fields

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4238 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 247fdc19ad256c7599f043aa49d3125d3a1f4356 1 parent a5d3e0c
Adrian Holovaty adrianholovaty authored
Showing with 23 additions and 0 deletions.
  1. +4 −0 django/newforms/widgets.py
  2. +19 −0 tests/regressiontests/forms/tests.py
4 django/newforms/widgets.py
View
@@ -189,6 +189,10 @@ def __iter__(self):
for i, choice in enumerate(self.choices):
yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i)
+ def __getitem__(self, idx):
+ choice = self.choices[idx] # Let the IndexError propogate
+ return RadioInput(self.name, self.value, self.attrs.copy(), choice, idx)
+
def __unicode__(self):
"Outputs a <ul> for this set of radio fields."
return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
19 tests/regressiontests/forms/tests.py
View
@@ -514,6 +514,25 @@
beatle J G George False
beatle J R Ringo False
+A RadioFieldRenderer object also allows index access to individual RadioInput
+objects.
+>>> w = RadioSelect()
+>>> r = w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
+>>> print r[1]
+<label><input type="radio" name="beatle" value="P" /> Paul</label>
+>>> print r[0]
+<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label>
+>>> r[0].is_checked()
+True
+>>> r[1].is_checked()
+False
+>>> r[1].name, r[1].value, r[1].choice_value, r[1].choice_label
+('beatle', u'J', 'P', 'Paul')
+>>> r[10]
+Traceback (most recent call last):
+...
+IndexError: list index out of range
+
# CheckboxSelectMultiple Widget ###############################################
>>> w = CheckboxSelectMultiple()
Please sign in to comment.
Something went wrong with that request. Please try again.