I have two model:
class Questions(models.Model):
question = models.TextField(verbose_name='Question')
def get_answers(self):
query = self.answers.select_related()
for q in query:
return q
def __unicode__(self):
return u'%s'%(self.question)
class Answers(models.Model):
answers = models.CharField(verbose_name='answer')
question = models.ForeignKey(Questions, related_name="answers")
def __unicode__(self):
return u'%s'%(self.answers)
I want to create a form that forms label created from Question and answers from Answers. I have created a form like this:
class QuestionForm(forms.Form):
def __init__(self,questions, *args, **kwargs):
self.questions = questions
for question in questions:
field_name = "question_%d" % question.pk
choices = []
for answer in question.answers.all():
choices.append((answer.pk,answer.answers))
field = forms.ChoiceField(label=question.question, required=True,
choices=choices, widget=forms.RadioSelect)
return super(QuestionForm, self).__init__(*args, **kwargs)
EDIT:
def my_view(request):
questions = Questions.objects.filter(......)
form = QuestionForm(questions)
return render_to_response('my_view.html',
{
'form':form
},
context_instance=RequestContext(request))
But with above view, there is not any form in template. What is wrong in my QuestionForm from?
Thanks in advance
If you change your class for this, your form will works