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]
. 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.
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. I think u would need to create the model Votecount as you defined to accomplish the task. However vote can remain in question and not in Votecount model. Just update vote in Question model and add to Votecount model everytime a person votes up a answer.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.