Monday, October 16, 2017

ModelFormset label

Hello all,

I'm trying to set a model forms label according to another model's field's value.

Now when the user loads the form, label's are shown as "Answer text" but I want it to show as the QuestionAnswer model's "question" foreign key's model's value. I.e. if the Question model's question_text value is "Question number 7" then the label for the form should be that. And as there are multiple questions on page the label should be according to those.

I can set the label correctly if in the view I zip the questions and answer_formset variables and then in the template loop through the zipped values and render the values separately. Is this the correct and only way to do it?

If you need clarification, please ask.

Form
class
AnswerForm(forms.ModelForm):
class Meta:
model = QuestionAnswer
fields = ['question', 'answer_text', 'questionnaire_key']
exclude = []
widgets = {
'question': HiddenInput,
'answer_text': RadioSelect(choices=CHOICES,
attrs={'required': 'True'}),
'questionnaire_key': HiddenInput,
}

Models
class QuestionAnswer(models.Model):
questionnaire_key = models.ForeignKey(Questionnaire, null=False, blank=False)
question = models.ForeignKey(Question, null=False, blank=False)
answer_text = models.CharField(max_length=1, null=False, default=None)
def __str__(self):
return self.answer_text

class Question(models.Model):
questionnaire = models.ForeignKey(Questionnaire)
question_text = models.CharField(max_length=200)
def __str__(self):
return self.question_text

View
def answerpage(request, questionnaire_pk):
AnswerFormSet = formset_factory(AnswerForm, extra=0)
questions = Question.objects.filter(questionnaire=questionnaire_pk)
qname = Questionnaire.objects.get(id=questionnaire_pk)
if request.method == 'POST':
answer_formset = AnswerFormSet(request.POST)
if answer_formset.is_valid():
for answer_form in answer_formset:
if answer_form.is_valid():
instance = answer_form.save(commit=False)
instance.answer_text = answer_form.cleaned_data.get('answer_text')
instance.question = answer_form.cleaned_data.get('question')
instance.questionnaire = answer_form.cleaned_data.get('questionnaire_key')
instance.save()
return redirect('main:calcs')
else:
return redirect('main:home')
else:
quest_id = request.session.get('questionnaire_key', 0)
question_data = [{'question': question,
'questionnaire_key': quest_id} for question in questions]

answer_formset = AnswerFormSet(initial=question_data)

context = {
'questions': questions,
'answer_formset': answer_formset,
'qname': qname,
}
return render(request, 'main/questionnaire_details2.html', context)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f5167b4a-a570-464b-ad31-4307aa2edc64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment