Render ChoiceField with radio buttons

I've constructed this form from a model.

class Configure_template(forms.Form):
    subject_type = forms.ChoiceField(choices=Subject_type.objects.all())

And I want to render this using radio buttons but I have problems with the for in the html,

Thanks for any suggestion.

Upvotes: 7

Views: 13021

Answers (2)

Neerajlal K
Neerajlal K

Reputation: 6828

If you want to show radiobuttons with choices from database use cannot use ChoiceField.

You have to use a ModelChoiceField.

subject_type = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=Subject_type.objects.all(), label='')

Upvotes: 4

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53981

Use a RadioSelect widget on your form field:

subject_type = forms.ChoiceField(choices=Subject_type.objects.all(),
    widget=forms.RadioSelect)

Upvotes: 15

Related Questions