无法指定“'2'”:“TakenQuiz.question”必须是“问题”实例

问题描述 投票:0回答:1

我试图在用户选择之后插入我从单选按钮获得的值。然后我想将它插入到另一个表中,我将用它来为学生准备最终结果。在我获取了所有值并创建了一个插入查询之后,我收到了这个错误。

enter image description here

models.朋友:

class Question(models.Model):

    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='questions')
    text = models.CharField('Question', max_length=500)

    def __str__(self):
        return self.text

class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='answers')
    text = models.CharField('Answer', max_length=255)
    is_correct = models.BooleanField('Correct answer', default=False)

    def __str__(self):
        return self.text

class TakenQuiz(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='taken_quizzes')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='taken_course')
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='taken_question')
    selected_choice = models.ForeignKey(Answer, on_delete=models.CASCADE, null=True)
    marks_obtained = models.DecimalField('Marks Obtained', default=0, decimal_places=2, max_digits=6)
    is_correct = models.BooleanField('Was this attempt correct?', default=False, null=False)
    date = models.DateTimeField(auto_now_add=True)

views.朋友:

@login_required
@student_required
def take_exam(request, pk):
    course = get_object_or_404(Course, pk=pk)
    student = request.user.student
    question = course.questions.filter()  
    #correct_answers = student.course_answers.filter(answer__question__quiz=course, answer__is_correct=True).count()
    total_questions = course.questions.count()
    choice = Answer.objects.filter()
    marks_obtainable = Details.objects.get(course_id=course)

    if request.method == 'POST':

        question_pk = request.POST.getlist('question_pk')
        choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_pk]

        zipped = zip(question_pk, choice_pk)

        for x, y in zipped:
            correct_answers = Answer.objects.filter(question_id=x,  is_correct=True).values('id').first()['id']
            #print(type(correct_answers))  
            #print(choice_pk) 
            print(x, y, correct_answers)        

            print(x, y, correct_answers)
            if int(y) == correct_answers:
                print("correct") #using this to comfirm the the conditional statement above
                z = TakenQuiz.objects.create(student=student, question=int(x), course=course, mark_obtained=marks_obtainable, is_correct=True)
                z.save()

                takenquiz = TakenQuiz()
                takenquiz.student = student
                takenquiz.question = x
                takenquiz.course = course
                takenquiz.selected_choice = y
                takenquiz.marks_obtained = marks_obtainable
                takenquiz.is_correct = True
                takenquiz.save()

            else:
                print("Not correct")
                z = TakenQuiz.objects.create(student=student, question=x, course=course, mark_obtained=marks_obtainable, is_correct=False)
                z.save()

    return render(request, 'classroom/students/take_exam_form.html', {
        'course': course,
        'question': question,
        'course': course,
        'total_questions': total_questions,
        'choice': choice,
        'marks_obtainable': marks_obtainable,
    })

take_exam_form.html:

<form method="post" novalidate>
        {% csrf_token %}
        {% for questions in question %}
        <input type="hidden" name="question_pk" value="{{ questions.pk }}">
        <h3 class="text-info">{{ questions.text|safe }}</h3>
    {% for choices in questions.answers.all %}
        <input class="form-check-input" type="radio" name="choice_pk{{ questions.pk }}" id="choices-{{ forloop.counter }}" value="{{ choices.pk }}">
        <label class="form-check-label" for="choices-{{ forloop.counter }}">
            {{ choices.text|safe }}
        </label>
      {% endfor %}
      {% endfor %}
          <button type="submit" class="btn btn-primary">Submit Now →</button>
</form>
django django-models django-forms django-views
1个回答
0
投票
question_pk = request.POST.getlist('question_pk')
question_obj = Question.objects.filter(id=int(question_pk)

while save



takenquiz.question = question_obj
© www.soinside.com 2019 - 2024. All rights reserved.