2017-04-15 7 views
2

投票(質問)のリストがあり、特定の投票に投票したかどうかを確認したいと考えています(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

私はこれについてどのように正確に行きますか?

答えて

0

最初に、対処する必要がある論理的および概略的な問題があります。

複数のオブジェクトを返す可能性のある一意でないフィールドquestion_textに基づいて質問をフィルタリングしています。 2人のユーザーがインクリメントしたいときに発生する可能性があることrace conditionsにつながる可能性のPythonを介して1でvotesをインクリメント、

question_text = models.CharField(max_length=80, unique=True) 

第二:

q = Question.objects.get(pk=question) 

するか、独自のquestion_textフィールドを作る:あなたはidで質問を取得する必要がありますどちらか同時に投票する。私は世論調査(質問)のリストを持っている

from django.db.models import F 

def poll_answer(request): 
    ... 
    choice = Choice.objects.get(id=answer) 
    choice.votes = F("votes") + 1 
    choice.save() 
    ... 

、および特定のユーザーが持っているかどうかを確認したいと思います:そこには、データベースレベルで増分を行うことができますどのジャンゴでFオブジェクトが存在する理由はここにありますその特定の投票に投票しました。

に設定できません。どの質問に投票したのですか?ユーザーが質問に投票すると、フィールドはvotesに増分されます。あなたはManyToManyFieldvotesフィールドを変更し、選択に票を入れているユーザーの格納を開始することができ、それを行うための

一つの方法:

class Question(models.Model): 
    text = models.CharField(max_length=80, unique=True) 

    def __str__(self): 
     return self.text 

class Choice(models.Model): 
    question = models.ForeignKey(Question) 
    choice = models.CharField(max_length=100) 
    votes = models.ManyToMany(User) 

ユーザーとして世論調査に投票している場合ので、あなたがチェックすることができます次の:

has_voted = request.user.choice_set.filter(question=question).exists() 

さらに、あなたがでご覧になる場合がありますので、ここでのアクションを投票するQuestionUserモデル間の中間モデルのように振る舞うことができますように私には思えますDjangoのドキュメントのセクション。

私はあなたの靴にあった場合、私は以下のような何かをするだろう:

class Question(models.Model): 
    text = models.CharField(max_length=80, unique=True) 
    voters = models.ManyToManyField(User, through="Vote") 

class Choice(models.Model): 
    question = models.ForeignKey(Question) 
    text = models.CharField(max_length=100) 

class Vote(models.Model): 
    question = models.ForeignKey(Question, on_delete=models.CASCADE) 
    user = models.ForeignKey(User, on_delete=models.CASCADE) 
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE) 
    voted_at = models.DateTimeField(auto_now_add=True) 
0

あり、私が投票のための別のモデルを作成することを好むだろうそれを行うには多くの方法、 は次のとおりです。あなたは簡単に得票数を数えることができるモデルと

class Vote(models.Model): 
    user = models.ForeignKey(User, unique=True) 

class Choice(models.Model): 
    question = models.ForeignKey(Question, related_name='choices') 
    choice_text = models.CharField(max_length=100) 
    votes = models.ForgeinKey(Vote) 

: そして、あなたの利益を持っています

if Vote.objects.filter(question=1, user=user_id).count() == 1 
user_id voted for question 1 already 
を:ユーザがすでに投票かどうかを検出するために、質問

Choice.objects.get(pk=PK).votes.count() 

に投票内容を正確に把握

0

あなたはこのようvoteビューを作成することができます上記のvoteビューは、選択したオプションのオプションIDを抽出しますが、選択したオプションなしの場合のようなエラーが発生した場合、その後exceptブロックを実行し、印刷します

def vote(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    try: 
     selected_choice = question.choice_set.get(pk=request.POST['choice']) 
    except: 
     return render(request, 'your_question_page.html', {'question': question, 
      'error_message': "Please select an option"}) 
    else: 
     selected_choice.votes += 1 
     selected_choice.save() 

     return redirect('polls:result_page_view', question_id=question_id) 

をそれ以外の場合は、選択したオプションの投票数を1増加させて保存します。 これがあなたを助けてくれることを願っています。

関連する問題