首页 > 解决方案 > 使用 Django 创建的多项选择测验应用程序的最佳模型选择是什么?

问题描述

我知道这里有一个与此类似的问题。我的问题建立在那个答案之上。

我正在构建一个网络应用程序,允许用户为教育目的创建测验。这意味着将创建数百个问题。

选项1

在引用的答案中,建议的方法是使用 ForeignKey 关系:

class Question(models.Model):
    prompt = models.TextField (max_length=300)
    answer = models.CharField (max_length=50) 

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice = models.CharField (max_length=300, blank=True)

但是,这种方法的问题是,如果我有 300 个问题,那将创建 1200-1800 或选择条目。随着创建的问题越来越多,这个数字将增加 10 个。作为一名教育工作者,我计划将其用于我的工作(我有几千个问题)并与其他老师共享该应用程序,他们都将添加和分享他们的自己的问题。我担心在更大范围内这会导致数据库出现性能问题。

选项 2

向我建议的是改用这种方法:

class Question(models.Model):
    prompt = models.TextField (max_length=300)
    answer = models.CharField (max_length=50) 

class ChoiceList(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice1 = models.CharField (max_length=300, blank=True)
    choice2 = models.CharField (max_length=300, blank=True)
    choice3 = models.CharField (max_length=300, blank=True)
    choice4 = models.CharField (max_length=300, blank=True)
    choice5 = models.CharField (max_length=300, blank=True)
    choice6 = models.CharField (max_length=300, blank=True)
    choice7 = models.CharField (max_length=300, blank=True)
    choice8 = models.CharField (max_length=300, blank=True)
    choice9 = models.CharField (max_length=300, blank=True)
    choice10 = models.CharField (max_length=300, blank=True)  

选项 3

这里还有第三种选择,即像第一个示例一样存储答案,但使用列表 [choice1,choice2,choice3...]。这样可以避免空值,也不会为每个问题条目创建 6 个平均选择条目。

通过为每个问题创建一个 ChoiceList 条目,是否有任何我没有预见到的问题?

哪种方法对我的应用程序最有效?

标签: djangodatabasemodel

解决方案


推荐阅读