Question 1:
answer1 *
answer2 *
Question 2:
answer3 *
answer4 *
# models.py class Question(models.Model): question_name = models.CharField(max_length=100) question_text = models.TextField() def __str__(self): return self.question_text class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.TextField() value = models.DecimalField(max_digits=5, decimal_places=2) def __str__(self): return self.choice_text
Последняя попытка в духе:
# forms.py class ChoiceTextForm(forms.Form): item_field = forms.ModelChoiceField(queryset=Choice.objects.none()) def __init__(self, question_id): super(ChoiceTextForm, self).__init__() self.fields['item_field'].queryset = Choice.objects.filter(question=question_id) # views.py def wvs_point_test(request): formset = forms.inlineformset_factory(Question, Choice, form=ChoiceTextForm, fields='choice_text', extra=0) question_id = 1 for form in formset: if request.method == 'POST': for question in Question.objects.all(): form = ChoiceTextForm(question_id, request.POST) question_id += 1 else: for question in Question.objects.all(): form = ChoiceTextForm(question_id) question_id += 1 return render( request, 'wvs_point/wvs_point_test.html', context={'formset': formset}) # html <form action="{% url 'wvs_point_result' %}" method="post"> {% csrf_token %} {{ formset }} </form>
Выдает “TypeError: ‘type’ object is not iterable” насчет “for form in formset:”. Дайте наводку, пожалуйста - никак не могу разобраться, как реализовать такую вроде бы нехитрую задачу.