如何仅选择用户尚未在Django中回答的问题 [英] How to select only questions not yet answered by a user in Django
本文介绍了如何仅选择用户尚未在Django中回答的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建测验,并且只想向用户显示自己未回答的问题。我该怎么做?
I am creating a quiz and would like to show to the user only questions that were not answered by himself. How do I do that?
models.py
class Questao(models.Model):
idQuestao = models.CharField(
max_length=7,
primary_key=True,
null=False,
verbose_name="ID da questão")
class Resposta(models.Model):
idQuestao = models.ForeignKey(
Questao,
on_delete=models.CASCADE,
verbose_name="ID da questão")
usuario = models.ForeignKey(
User,
on_delete=models.CASCADE,
verbose_name="Usuário")
views.py
questao = Questao.objects.filter(tipoQuestao=1, resposta__usuario=request.user)\
.exclude(idQuestao=Resposta.objects.filter())\
.order_by("?")\
.first()
编辑
当我进入测验时,我被问到问题E200501回答,但是
When I enter the quiz, I was asked the question E200501 to answer, but the one that was answered was the E200503.
推荐答案
我假设只要用户回答问题<$ c如果只想返回用户尚未回答的 Questao
,而只需要,则会创建$ c> Resposta
对象exclude()
I assume that whenever user answers to a question Resposta
object is created if you simply want to return the Questao
that user has not answered yet you just need exclude()
questao = Questao.objects.exclude(resposta__usuario=request.user)
这篇关于如何仅选择用户尚未在Django中回答的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文