tags:

views:

160

answers:

1

I can set the help_text attribute on any form field, but is it possible to set help_text on the choices used for a RadioSelect()?

I'd looking for a clean way to show some help information under each radio button.

Below is the code for the model and the form, I can render the name attribute in a template with the label, input element and help text. I'd also like to be able to render membership_type attribute with a label ('Membership Type'), radio buttons ('open membership' and 'closed membership'), and help text associated to each radio element ('anyone can join this group' and 'only select members can join this group').

class Group(models.Model):
  MEMBERSHIP_CHOICES = (
    ('O', 'Open membership'),
    ('C', 'Closed membership'),
  )

  name = models.CharField(max_length=255)
  membership_type = models.CharField(max_length=1, choices=MEMBERSHIP_CHOICES, default="O")

class GroupForm(forms.ModelForm):
  name = forms.CharField(label="Group name", help_text="Enter a name for your new group")

  class Meta:
    model = Group
    widgets = { "membership_type": forms.RadioSelect }
A: 

Assuming you're using RadioSelect as a widget for forms.ChoiceField, you can do something like:

choices = (('1', 'First help_text here'),
           ('2', 'Second help_text here'),
           ('3', 'Third help_text here'),
          )

class MyForm(forms.Form):
    ...
    choice = forms.ChoiceField(widget = RadioSelect, choices = choices)

This isn't a strict use of help_text but it should get the job done in most cases.

Rishabh Manocha
In your example you've setup an id and label attribute. I already have both of those, I'm looking to add a third attribute; help_text.
mountainswhim
Maybe if you showed us some code, we'd be able to help more. Right now, I don't understand what it is you're trying to achieve that cannot be done using the `id` and `label` attributes.
Rishabh Manocha
I updated the question with more information and code snippets, thanks for looking at this.
mountainswhim