D
D
Dev_nab2020-01-13 01:54:00
Django
Dev_nab, 2020-01-13 01:54:00

Django - How to make testing (questionnaire) with answer options and how to store them?

It is necessary to make a test in the style:
1. Does the earth have the shape of a ball?
- Yes - No
2. Is a mosquito an insect?
- Yes - No
, etc.
How to store answers so that the test can be taken several times by different users and how to check answers?
Tell me in which direction to dig (I have an idea, but I'm afraid it's cumbersome and clumsy).
models.py

class Test(models.Model):
    question = models.CharField('Текст вопроса', max_length=150)
    answer = models.BooleanField('Ответ', default=False) # Да/Нет

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-01-13
@syschel

models.py

class Test(models.Model):
    question = models.CharField('Текст вопроса', max_length=150)
    answer = models.BooleanField('Ответы', default=False) # Да/Нет (тут храним правильный ответ)

class ResultTests(models.Model):
    test = models.ForeignKey(Test, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    result = models.BooleanField('Результат', default=False) # true если ответ был верный

When creating questions in the admin panel, immediately indicate which answer is correct, but display only the "text of the question" and the choice of Yes / No to users. When the user answers questions, check whether the result of the answer field is equal to what the user has chosen. In the ResultTests model, we create a new object, passing the current user, the current question and the result of the answer (true - if the answer is correct) there.

M
mt_max, 2020-01-13
@mt_max

I do not pretend to be true, but in my project I implemented the following models:

from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib import admin


class Profile(models.Model):
    Name = models.CharField(max_length=200, verbose_name='Название теста')
    WorkTime = models.IntegerField(verbose_name='Время выполнения (мин)')
    QuestionsCount = models.IntegerField(verbose_name='Количество вопросов')
    Statisfactorily = models.IntegerField(verbose_name='Удовлетворительно')
    Good = models.IntegerField(verbose_name='Хорошо')
    Perfect = models.IntegerField(verbose_name='Отлично')

    class Meta:
        verbose_name = 'Тесты'
        verbose_name_plural = 'Тесты'



    def __str__(self):
        return self.Name


class Question(models.Model):
    ProfileId = models.ForeignKey(Profile, on_delete=models.CASCADE, verbose_name='Тест')
    Text = models.TextField(verbose_name='Текст вопроса')
    Weight = models.FloatField(default=1, verbose_name='Вес')

    class Meta:
        verbose_name = 'Вопрос'
        verbose_name_plural = 'Вопросы'

    def __str__(self):
        return self.Text


class Answer(models.Model):
    QuestionId = models.ForeignKey(Question, on_delete=models.CASCADE)
    Text = models.CharField(max_length=300)
    IsRight = models.BooleanField()

    class Meta:
        verbose_name = 'Вариант ответа'
        verbose_name_plural = 'Варианты ответа'

    def __str__(self):
        return self.Text

class Result(models.Model):
    ProfileId = models.ForeignKey(Profile, on_delete=models.CASCADE, verbose_name='Тест')
    UserName = models.CharField(max_length=300, verbose_name="ФИО")
    DateTime = models.DateTimeField(auto_now_add=True, blank=True, verbose_name="Время завершения")
    Rating =models.FloatField(verbose_name="Проценты")

    class Meta:
        verbose_name = 'Результат'
        verbose_name_plural = 'Результаты'




class QuestionsInline(admin.TabularInline):
    model = Answer

@admin.register(Question)
class BookAdmin(admin.ModelAdmin):
    inlines = [QuestionsInline]


@admin.register(Result)
class ResultAdmin(admin.ModelAdmin):
    list_display=("ProfileId", "DateTime", "UserName", "Rating")

    def has_add_permission(self, request):
        return False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question