django - 使用 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 条目,是否有任何我没有预见到的问题?
哪种方法对我的应用程序最有效?
解决方案
推荐阅读
- java - 嵌套对象的不同注释实现
- rtos - 使用 Shell 命令执行 VxWorks 653 应用程序
- c# - System.InvalidOperationException: '对象当前正在其他地方使用。'
- javascript - 如何使用 React 使用 axios 和使用 aws-sdk 预签名将文件上传到 S3 存储桶?
- json - 为多个目标主机创建 ansible 清单
- c++ - 以文件为参数的 C++ .exe
- azure-devops - 通过 REST API 将任务作为子项添加到故事中
- django - Django 3,我更改 DEBUG = False,当我发送信件时,图像消失了,为什么会发生这种情况?
- javascript - 在引导程序的 b 表中设置模板的 id
- react-native - 反应原生触摸多选