tags:

views:

29

answers:

2

I am trying to write a quiz application.i have the follwing model.

class Question(db.Model):
    question=db.StringProperty(required=True)
    answer_1=db.StringProperty(required=True)
    answer_2=db.StringProperty(required=True)
    answer_3=db.StringProperty(required=True)
    answer_4=db.StringProperty(required=True)
    correct_answer=db.StringProperty(choices=['1','2','3','4'])

and the following form

class QuestionForm(ModelForm):
    class Meta:
        model=Question

which served me well for creating forms for submitting new questions. Now i want the stored Questions in the database to be presented in form for a Quiz to the user.The above form would generate the form as having

<input type="text">

while i want them to have radio boxes how do i achive the same? do i need a seperate form class ?

A: 

You can specify a widget for drawing the Quiz form as radio boxes - you can see more about widgets at http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets

As for your question about the separate form class, it's up to you - do you want to have 2 separate ways of showing a form to the user (with radio button and with a text field), or do you want just the radio buttons - either way it's up to you to decide.

aeter
+1  A: 

You could also store the possibie answers in another model, admin them through an inline admin (whcih would give you more flexibility, because the number of answers doesnt always have to be the same then), and the use a foreign key field for the correct answer (which will render as dropdown or radio boxes if you wish)!

lazerscience