django过滤当前用户投票

时间:2014-12-21 02:58:48

标签: python django views django-queryset

我有一个模特:

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=120)
    description = models.TextField()
    answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True)
    post_date = models.DateTimeField(auto_now=True)
    vote = models.IntegerField(default=0)

    def __unicode__(self):
        return self.title

我正在制作论坛,用户可以发帖提问。我想要的是如果问题是有用的,那么其他用户可以投票但只有一次。如果用户已经投票一次并尝试再次投票,则会显示显示消息,表示您无法投票两次。我已经为此写了一个视图,除了两次投票验证之外,其他一切都有效。

以下是我的观点:

def VoteCountView(request, pk):

    ques = Question.objects.get(id=pk)
    cpk = ques.category.id
    valid_user = ques.user.username

    if request.user.is_authenticated():
        if request.user.is_general_user:
            if request.user.username == valid_user:
                messages.warning(request, "You cannot vote your own question")
                return redirect("question-detail", pk, cpk)
            else:
                vote_limit = Question.objects.values_list("vote", flat=True).filter(pk=pk).filter(user=request.user)[0]
                if vote_limit < 1:
                    vote_count = ques
                    count = vote_count.vote
                    count +=1
                    vote_count.vote = count
                    vote_count.user = request.user
                    vote_count.save()
                    return redirect("question-detail", pk, cpk)
                else:
                    messages.warning(request,"You cannot vote twice")
                    return redirect("question-detail", pk, cpk)
        else:
            messages.warning(request,"You are not allowed to vote a question")
            return redirect("question-detail", pk, cpk)
    else:
        messages.warning(request, "You must login/sign up to vote.")
        return redirect("question-detail", pk, cpk)

我想在这里实现的是vote_limit = Question.objects.values_list("vote", flat=True).filter(pk=pk).filter(user=request.user)[0]。我认为我使用filter(user=request.user)的最后一个过滤器不起作用,因为它已经过了发布问题的用户的过滤。

我可以通过相同的模型做到这一点,或者我必须制作不同的模型才能实现这一目标。像:

class VoteCount(models.Model):
    user = models.ForeignKey(User)
    title = models.ForeignKey(Question)
    vote = models.IntegerField(default=0)

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

你可以简单地检查一下

VoteCount.objects.filter(user=request.user, title=ques).exists()

知道用户是否已经投了这样的问题

答案 1 :(得分:0)

只需使用exists()来检查用户是否已投票。我想你需要创建你定义的模型Votecount来完成任务。但是投票仍然存在问题,而不是Votecount模型。只需更新问题模型中的投票,并在每个人投票给答案时添加到Votecount模型。

def VoteCountView(request, pk): 
    ques = Question.objects.get(id=pk)
    cpk = ques.category.id
    valid_user = ques.user.username

    if request.user.is_authenticated():
        if request.user.is_general_user:
            if request.user.username == valid_user:
                messages.warning(request, "You cannot vote your own question")
                return redirect("question-detail", pk, cpk)
            elif VoteCount.objects.filter(user=request.user, title=ques).exists():
                messages.warning(request,"You cannot vote twice")
                return redirect("question-detail", pk, cpk)
            else:
                ques.vote +=1
                ques.save()
                v=Votecount(title=ques, user=request.user)
                v.save()    
        else:
            messages.warning(request,"You are not allowed to vote a question")
            return redirect("question-detail", pk, cpk)
    else:
        messages.warning(request, "You must login/sign up to vote.")
        return redirect("question-detail", pk, cpk)