简体   繁体   English

django通过当前用户筛选来投票

[英]django filter by current user to vote

I have a model: 我有一个模型:

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

I am making a forum kind of thing where user can post question. 我正在创建一个论坛,用户可以在其中发布问题。 What I want is if the question is useful then other user can vote the question but only once. 我想要的是,如果该问题有用,那么其他用户只能对该问题投一次票。 If user has voted the question once and tries to vote again it show show message saying you cannot vote twice. 如果用户已投票一次,然后尝试再次投票,则会显示显示消息,提示您无法两次投票。 I have written a view for this where everything works except the validation for voting twice. 我为此写了一个观点,其中除了两次投票的验证外,其他所有东西都起作用。

Here is my view: 这是我的看法:

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)

What I am trying to achieve here is vote_limit = Question.objects.values_list("vote", flat=True).filter(pk=pk).filter(user=request.user)[0] . 我在这里想要实现的是vote_limit = Question.objects.values_list("vote", flat=True).filter(pk=pk).filter(user=request.user)[0] I think my last filter what I am using filter(user=request.user) is not working because its filtering by the user that has posted the question. 我认为我正在使用的最后一个过滤器filter(user=request.user)无法正常工作,因为它已由发布问题的用户过滤。

Can I do this by same model or I have to make different model to achieve this. 我可以通过相同的模型来完成此操作,还是必须制作不同的模型来实现此目的? Like: 喜欢:

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

Can anyone help me ?? 谁能帮我 ??

you could simply check 你可以简单地检查

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

to know if a user has already voted such question 知道用户是否已经投票过这样的问题

Just use exists() for checking if a user has already cast a vote or not. 只需使用exist()来检查用户是否已经投票。 I think u would need to create the model Votecount as you defined to accomplish the task. 我认为您需要按照定义的方式创建模型Votecount才能完成任务。 However vote can remain in question and not in Votecount model. 但是,投票可能仍然存在问题,而不是在Votecount模型中。 Just update vote in Question model and add to Votecount model everytime a person votes up a answer. 每次有人投票回答一个答案时,只需更新Question模型中的投票并添加到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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM