Django

Code

Changeset 4238

Show
Ignore:
Timestamp:
12/26/06 16:56:53 (2 years ago)
Author:
adrian
Message:

newforms: Implemented RadioFieldRenderer?.getitem(), which allows for index lookup on radio fields

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/widgets.py

    r4235 r4238  
    190190            yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i) 
    191191 
     192    def __getitem__(self, idx): 
     193        choice = self.choices[idx] # Let the IndexError propogate 
     194        return RadioInput(self.name, self.value, self.attrs.copy(), choice, idx) 
     195 
    192196    def __unicode__(self): 
    193197        "Outputs a <ul> for this set of radio fields." 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4236 r4238  
    514514beatle J G George False 
    515515beatle J R Ringo False 
     516 
     517A RadioFieldRenderer object also allows index access to individual RadioInput 
     518objects. 
     519>>> w = RadioSelect() 
     520>>> r = w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 
     521>>> print r[1] 
     522<label><input type="radio" name="beatle" value="P" /> Paul</label> 
     523>>> print r[0] 
     524<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label> 
     525>>> r[0].is_checked() 
     526True 
     527>>> r[1].is_checked() 
     528False 
     529>>> r[1].name, r[1].value, r[1].choice_value, r[1].choice_label 
     530('beatle', u'J', 'P', 'Paul') 
     531>>> r[10] 
     532Traceback (most recent call last): 
     533... 
     534IndexError: list index out of range 
    516535 
    517536# CheckboxSelectMultiple Widget ###############################################