Hi Guys,

This might get lengthy but I am trying to get some insight on the best
way to generate some dynamic forms in a template. The project is a
survey. Here is my questions model:

class Question(models.Model):
        TRUEFALSE = 1 # a radio button yes/no question
        MULTIPLEANSWER = 2 # checkbox question
        SINGLEANSWER = 3 # radio button answer
        COMMENT = 4 # textarea box for comments

        QUESTION_CHOICES = (
                (TRUEFALSE, 'Yes/No question'),
                (MULTIPLEANSWER, 'Multiple answer question'),
                (SINGLEANSWER, 'Single answer'),
                (COMMENT, 'Comment box')
        )

        question_type = models.IntegerField(choices=QUESTION_CHOICES,
default=TRUEFALSE)
        survey = models.ForeignKey(Survey)
        title = models.CharField(max_length=250)

        def __unicode__(self):
                return self.title

Questions are built using the django admin and they can select the
type of question it will be via the QUESTION_CHOICES. In my template I
am testing for the type of question but I want to display the proper
form fields for this question. So, a True/False will be radio widget,
multiple answer will be checkbox and so on, I will need to do this
dynamically of course so i can show the question with the widget type
from the constants in my model. How can I do this?

Currently I have something somewhat working...  :

class TrueFalseForm(forms.Form):
        def __init__(self, questions, *args, **kwargs):
                super(TrueFalseForm, self).__init__(*args, **kwargs)
                for i, question in enumerate(questions):
                        self.fields['answer'] = 
forms.ChoiceField(widget=forms.RadioSelect
(), choices=[['%d % i', question]], label=question)


Any suggestions would be much 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