所以这是我的表格:
class QuestionForm(forms.ModelForm):
answer = forms.ChoiceField(required=True, widget=forms.RadioSelect())
def __init__(self, question=None, *args, **kwargs):
super(QuestionForm, self).__init__(*args, **kwargs)
self.fields['answer'].choices = [(a.answer, a.answer) for a in Answer.objects.filter(question=question)]
class Meta:
model = Questions
fields = ('answer', 'importance' )
当用户选择answer
它时,它会显示所有Answer
对象。以下是我的看法:
def questions(request):
new_forms = []
q = Questions.objects.all()[0]
if request.method =='POST':
#form = QuestionForm(request.POST or None)
for forms in new_forms:
if forms.is_valid():
new = forms.save(commit=False)
#get the user's inputs to the QuestionForm and save them
UserAnswer(user=request.user, answer=new.cleaned_data['answer'], question=new.question)
UserAnswer.save()
else:
instance=Questions.objects.all().filter(owner=request.user)[0]
#form = QuestionForm(question=q, instance=q)
for a in Questions.objects.all():
new_forms.append(QuestionForm(question=a, instance=a))
return render_to_response("questions/base.html", locals(), context_instance=RequestContext(request))