投票(質問)のリストがあり、特定の投票に投票したかどうかを確認したいと考えています(User
)。ここに私のモデルです:ユーザーが特定の投票に投票しているかどうかを確認
class Question(models.Model):
has_answered = models.ManyToManyField(User)
question_text = models.CharField(max_length=80)
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=100)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
ここに私の見解は、世論調査の際に、ユーザーの投票です:
def poll_answer(request):
if request.method == 'POST':
answer = request.POST.get('answer')
question = request.POST.get('question')
q = Question.objects.get(question_text=question)
choice = Choice.objects.get(id=answer)
choice.votes += 1
choice.save()
...
私は、ドキュメントを読んだ後、私はあると信じて私のQuestion
モデルにManyToMany
フィールドを追加しました特定の質問に投票したユーザーのリストをリンクするには正しい方法ですが、正直であるかどうかはわかりません。最終的な目標は、テンプレートに次のようなものを入れることです:if request.user in question.has_answered: don't display the poll
私はこれについてどのように正確に行きますか?