Getting radio boxes with ModelForm

2,994 views
Skip to first unread message

Andy Dietler

unread,
Jun 8, 2009, 3:23:06 PM6/8/09
to Django users
I'm trying to make one of the fields in my model display radio buttons
with the options 1-5. I can't find a way to do this with a model form
and I can't get anything I find in documentation to work properly.

What I have below results in me getting the following error:
TypeError: __init__() got an unexpected keyword argument 'widget'

Any help would be much appreciated. - Andy

rating_choices = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)

class Rating(models.Model):
rating = models.IntegerField(widget=forms.RadioSelect
(choices=rating_choices),label="")

Daniel Roseman

unread,
Jun 8, 2009, 4:18:21 PM6/8/09
to Django users
You're confusing models and forms. You can't set things like widgets
on the model, you do that on a form.

You need to define a model form and override the definition of
'rating' to include the widget parameter. I presume you're looking to
use this in the admin, in which case you need to set the form
attribute of your rating admin class to the new form you've defined.
--
DR.

Andy Dietler

unread,
Jun 8, 2009, 4:55:02 PM6/8/09
to Django users
Thanks.

How do I overwrite the definition of rating, like this?:

class Rating(models.Model):
rating = models.IntegerField()


class ShowForm_Rate(forms.ModelForm):
class Meta:
model = Rating
rating = forms.ChoiceField(widget=forms.RadioSelect
(choices=rating_choices),label="")

On Jun 8, 1:18 pm, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:

Alex Gaynor

unread,
Jun 8, 2009, 4:58:16 PM6/8/09
to django...@googlegroups.com
Move the field definition from the Meta class out into the outer class and you're good to go.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero
Reply all
Reply to author
Forward
0 new messages