I have three models class as below.
from django.forms import ModelForm
RATING_CHOICES = ((1, "Weak"), (2, "Average"), (3, "Good"), (4, "Excellent"))
class Question(models.Model):
text = models.CharField(max_length=100)
def __str__(self):
return self.text
class Teacher(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
answer = models.IntegerField(choices=RATING_CHOICES, default=1)
in the template I have access to my fields in this way:
{% csrf_token %}
<select name="teacher_selected">
{% for teacher in teacher %}
<option value="{{teacher}}">{{teacher}}</option>
{% endfor %}
</select>
{% for question in question %}
<ol>{{ question.text }}</ol>
{% for choice in rating_choices %}
<input type="radio" name="question_{{question.id}}" value="{{choice.0}}">{{choice.1}}
{% endfor %}
{% endfor %} <br>
<input type="submit" value="Vote">
</form>
Now I know how to get the value of Teacher class teacher = request.POST.get(‘teacher_selected’), but I do not know how to store answers since it is a little different compare to teacher.
I asked this question in StackOverFlow but did not get the clear answer. They just gave me hints. For example, use formset or linking part 4 of Django documentation.
I need a proper views.py for my codes.