Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #5993 -- Made `RadioSelect` easier to subclass by giving it a d…

…efault `renderer` as a class attribute.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6717 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f110da91dbfa2c9db7e6ef2082b4eb28a7741737 1 parent 183a457
@gdub gdub authored
View
8 django/newforms/widgets.py
@@ -317,11 +317,13 @@ def render(self):
% force_unicode(w) for w in self]))
class RadioSelect(Select):
+ renderer = RadioFieldRenderer
def __init__(self, *args, **kwargs):
- self.renderer = kwargs.pop('renderer', None)
- if not self.renderer:
- self.renderer = RadioFieldRenderer
+ # Override the default renderer if we were passed one.
+ renderer = kwargs.pop('renderer', None)
+ if renderer:
+ self.renderer = renderer
super(RadioSelect, self).__init__(*args, **kwargs)
def get_renderer(self, name, value, attrs=None, choices=()):
View
10 tests/regressiontests/forms/widgets.py
@@ -663,6 +663,16 @@
<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
<label><input type="radio" name="beatle" value="R" /> Ringo</label>
+Or you can use custom RadioSelect fields that use your custom renderer.
+>>> class CustomRadioSelect(RadioSelect):
+... renderer = MyRenderer
+>>> w = CustomRadioSelect()
+>>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
+<label><input type="radio" name="beatle" value="J" /> John</label><br />
+<label><input type="radio" name="beatle" value="P" /> Paul</label><br />
+<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
+<label><input type="radio" name="beatle" value="R" /> Ringo</label>
+
A RadioFieldRenderer object also allows index access to individual RadioInput
objects.
>>> w = RadioSelect()
Please sign in to comment.
Something went wrong with that request. Please try again.