我在django中创建测验应用,我的django模型问题是这样的,
class Question(models.Model):
questions = models.CharField(max_length=50, unique=True)
choice1 = models.CharField(max_length=50, unique=True)
choice2 = models.CharField(max_length=50, unique=True)
choice3 = models.CharField(max_length=50, unique=True)
choice4 = models.CharField(max_length=50, unique=True)
correct_answer = models.CharField(max_length=50, unique=True)
这样很好或保存postgres数组中的四个选项或将选项保存在单独的表中。
答案 0 :(得分:6)
对于正确规范化的关系数据库模式,您需要在Choice
上使用外键的不同Question
模型:
class Question(models.Model):
question = models.CharField(...)
class Choice(models.Model):
question = models.ForeignKey("Question", related_name="choices")
choice = modelsCharField("Choice", max_length=50)
position = models.IntegerField("position")
class Meta:
unique_together = [
# no duplicated choice per question
("question", "choice"),
# no duplicated position per question
("question", "position")
]
ordering = ("position",)
然后,您可以使用Question
获得myquestion.choices.all()
个选项(并使用Choice
从mychoice.question
获取问题。)
请注意,这不会对问题的选择数量施加任何限制,甚至不会要求问题至少有一个相关的选择。
除非你有非常令人信服的理由不这样做,否则在使用关系数据库时你需要一个正确规范化的模式(rdbms不仅仅是bitbuckets,它们提供了很多的有用功能 - 只要你有一个合适的架构,那就是)。
答案 1 :(得分:0)
这是它的摘要
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
class Quiz(models.Model):
name = models.CharField(max_length=1000)
questions_count = models.IntegerField(default=0)
description = models.CharField(max_length=70)
created = models.DateTimeField(auto_now_add=True,null=True,blank=True)
slug = models.SlugField()
roll_out = models.BooleanField(default=False)
class Meta:
ordering = [‘created’,]
verbose_name_plural =”Quizzes”
def __str__(self):
return self.name
class Question(models.Model):
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
label = models.CharField(max_length=1000)
order = models.IntegerField(default=0)
def __str__(self):
return self.label
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
text = models.CharField(max_length=1000)
is_correct = models.BooleanField(default=False)
def __str__(self):
return self.text
class QuizTakers(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
correct_answers = models.IntegerField(default=0)
completed = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
class Response(models.Model):
quiztaker = models.ForeignKey(QuizTakers, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
answer = models.ForeignKey(Answer,on_delete=models.CASCADE,null=True,blank=True)
def __str__(self):
return self.question.label
@receiver(post_save, sender=Quiz)
def set_default_quiz(sender, instance, created,**kwargs):
quiz = Quiz.objects.filter(id = instance.id)
quiz.update(questions_count=instance.question_set.filter(quiz=instance.pk).count())
@receiver(post_save, sender=Question)
def set_default(sender, instance, created,**kwargs):
quiz = Quiz.objects.filter(id = instance.quiz.id)
quiz.update(questions_count=instance.quiz.question_set.filter(quiz=instance.quiz.pk).count())
@receiver(pre_save, sender=Quiz)
def slugify_title(sender, instance, *args, **kwargs):
instance.slug = slugify(instance.name)
答案 2 :(得分:0)
如果你使用的是 postgres sql,你可以使用 arryfield
This is a line.
Putting a "newline", as here, before a \begin will cause the error. \\
\begin{figure}[h]
....
\end{figure}
答案 3 :(得分:0)
最好的方法是:
ANSWER_CHOICES = (
("choice_1", "Answer_1"),
("choice_2", "Answer_2"),
("choice_3", "Answer_3"),
("choice_4", "Answer_4"),
("choice_5", "Answer_5"),
("choice_6", "Answer_6"),
)
class Question(models.Model):
questions = models.CharField
(
max_length=50,
unique=True,
choices=ANSWER_CHOICES
)
correct_answer = models.CharField(max_length=50, unique=True)