From f110da91dbfa2c9db7e6ef2082b4eb28a7741737 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Mon, 26 Nov 2007 02:39:48 +0000 Subject: [PATCH] Fixed #5993 -- Made `RadioSelect` easier to subclass by giving it a default `renderer` as a class attribute. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6717 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/widgets.py | 8 +++++--- tests/regressiontests/forms/widgets.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 350b878af9e..a2d5f52ff11 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -317,11 +317,13 @@ class RadioFieldRenderer(StrAndUnicode): % 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=()): diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index cb1d0846312..81254a149ec 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -663,6 +663,16 @@ You can create your own custom renderers for RadioSelect to use.
+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'))) +
+
+
+ + A RadioFieldRenderer object also allows index access to individual RadioInput objects. >>> w = RadioSelect()