django - 用于测验的 Django 视图
问题描述
我是 Django 的新手,我想制作测验应用程序,但我遇到了问题。我创建了 3 个模型(测验、问题、选择)。我想编写一个函数来返回具有相同测验标题的问题。
我试过这个
视图.py
def detail(request):
sets = Quiz.objects.all()
question = Question.objects.filter(sets.title)
return render(request,'App/appdetail.html',{'question':question})
模型.py
class Quiz(models.Model):
title = models.CharField(max_length=20)
description = models.CharField(max_length=100)
def __str__(self):
return self.title
class Question(models.Model):
set = models.ForeignKey(Quiz,on_delete=models.CASCADE)
question_txt = models.CharField(max_length=100)
def __str__(self):
return self.question_txt
class Choice(models.Model):
question = models.ForeignKey(Question,on_delete=models.CASCADE)
choice_txt = models.CharField(max_length=20)
boolean = models.BooleanField(default=False)
def __str__(self):
return self.choice_txt
错误信息
解决方案
set
您可以通过过滤模型中的测验外键问题来获得具有相同测验标题的所有问题Question
。
question = Question.objects.filter(set__title='your_quiz_title')
推荐阅读
- scikit-learn - 如何使用包含 OneHotEncode 和 LightGBM 的管道预测验证集上的目标?
- password-hash - ActiveCollab - 忘记管理员密码 - 无法通过 phpmyadmin 重置
- python-3.x - 名称“是”未定义
- java - 如何修复 IntelliJ 中的 NoClassDefFoundError: org.slf4j.LoggerFactory (但 Eclipse 工作正常)
- haskell - 为什么 quickCheck 会创建单位列表
- dart - 错误:名称“E”不是类型,因此不能用作类型参数,为什么以及如何创建泛型函数?
- java - JavaFX:右键单击 TableColumn 禁用调整大小
- php - 如何解决这个问题以及如何提高php编程水平
- c# - 如何订阅和传递子可观察集合的集合更改事件?
- python - 在 Keras 自定义层中获取批量大小并使用 tensorflow 操作 (tf.Variable)