I use the following code that I got from a django snippet, which works
great if you want to have radio buttons with different types of fields
associated with them

from django                                                     import forms
from django.utils.encoding                      import force_unicode

class ChoiceWithOtherRenderer(forms.RadioSelect.renderer):
        def __init__(self, *args, **kwargs):
                super(ChoiceWithOtherRenderer, self).__init__(*args, **kwargs)

        def __iter__(self):
                for inp in self.choices:
                        id = '%s_%s' % (self.attrs['id'], inp[0]) if 'id' in 
self.attrs
else ''
                        label_for = ' for="%s"' % id if id else ''
                        checked = '' if not force_unicode(inp[0]) == self.value 
else
'checked="true" '
                        html = '<label%s><input type="radio" id="%s" value="%s" 
name="%s"
%s/> %s</label> %%s' %  \
                        (label_for, id, inp[0], self.name, checked, inp[1])

                        yield html

class ChoiceWithOtherWidget(forms.MultiWidget):
        def __init__(self, choices):
                widgets = [forms.RadioSelect(choices=choices,
renderer=ChoiceWithOtherRenderer),]
                for choice in choices:
                        widgets.append(choice[2])

                super(ChoiceWithOtherWidget, self).__init__(widgets)

        def decompress(self, value):
                if not value:
                        return [None, None]
                return value

        def format_output(self, rendered_widgets):
                render = []
                for widget in rendered_widgets[1:]:
                        render.append(widget)

                render = tuple(render)
                return rendered_widgets[0] %    render

class ChoiceWithOtherField(forms.MultiValueField):
    def __init__(self, *args, **kwargs):
                fields = [
        
forms.ChoiceField(widget=forms.RadioSelect(renderer=ChoiceWithOtherRenderer),
*args, **kwargs),
                ]
                widget = ChoiceWithOtherWidget(choices=kwargs['choices'])
                kwargs.pop('choices')
                self._was_required = kwargs.pop('required', True)
                kwargs['required'] = False
                super(ChoiceWithOtherField, self).__init__(widget=widget,
fields=fields, *args, **kwargs)

    def compress(self, value):
                if self._was_required and not value or value[0] in (None, ''):
                        raise 
forms.ValidationError(self.error_messages['required'])
                if not value:
                        return [None, u'']
                return (value[0], value[1] if force_unicode(value[0]) ==
force_unicode(self.fields[0].choices[-1][0]) else u'')


So in forms.py I create a form something like this.


BUDGET_CHOICES = [('Budget'   , 'Budget',    forms.TextInput() ),
                  ('No Budget', 'No Budget', forms.RadioSelect )
                 ]


class PaymentForm(forms.Form):
    budget          = ChoiceWithOtherField ( choices =
BUDGET_CHOICES )



This works great,  until I created a FormsView class

class Payment( FormView ):
    template_name = 'leadb/lb_payment.html'
    form_class    = PaymentForm


    def form_valid(self, form):
        """

then I got
Exception Type:         ValueError
Exception Value:

too many values to unpack

Exception Location:     C:\Python27\lib\site-packages\django-1.3.1-
py2.7.egg\django\forms\fields.py in valid_value, line 680

I am passing three choices and it expects 2. I am trying to figure out
how to get around this. Any advice appreciated.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to