如何实现投票确认系统?

时间:2021-03-15 14:30:20

I'm trying to create a simple retriction system so the users can't vote twice on a simple poll (mostly like the example poll in the django tutorial) bit I can't seem to be able to find an approach that I like.

我正在尝试创建一个简单的检索系统,这样用户就不能对一个简单的轮询(主要像django教程中的示例轮询)进行两次投票。

The one that I like the most is having a User FK in the Choice model and add the users there, like this:

我最喜欢的是在Choice模型中加入一个用户FK,并在其中添加用户,如下所示:

models.py

models.py

vote = models.ForeignKey(User)

views.py

views.py

def vote(request):
    # Some validations and stuff...
    vote.add(request.user)

That way I can restrict the votes to 1 per choice, but I wanted to restrict it to 1 per poll. Imagine the situation: you have a poll that has 5 choices, with this validation, the user can only vote 1 time, but 1 time per choice, which means he/she can vote 5 times.

这样的话,我就可以将投票限制在每个选项1,但是我想将投票限制在每个投票1。设想这样的情况:您有一个有5个选项的投票,通过这个验证,用户只能投1次,但每个选项只能投1次,这意味着他/她可以投5次。

What would you recommend for making a system that allow only 1 vote per poll? I you need the models or something I'll paste them, it's an opensource project.

对于一个每次投票只允许一票的制度,你有什么建议?如果你需要模型或者其他东西,我会粘贴它们,这是一个开源项目。

1 个解决方案

#1


2  

On your Poll model, add a ManyToManyField to User, representing which users have voted in which polls. For each poll you want to display, check if this poll is in the current user's list of polls. If it is, do not allow them to vote.

在您的轮询模型中,向用户添加许多tomanyfield,代表哪些用户在哪个投票中投票。对于要显示的每个轮询,请检查该轮询是否在当前用户的轮询列表中。如果是,不要让他们投票。

And when the current user votes in a poll, add that poll to the current user's list of polls.

当当前用户在轮询中投票时,将该轮询添加到当前用户的轮询列表中。

#1


2  

On your Poll model, add a ManyToManyField to User, representing which users have voted in which polls. For each poll you want to display, check if this poll is in the current user's list of polls. If it is, do not allow them to vote.

在您的轮询模型中,向用户添加许多tomanyfield,代表哪些用户在哪个投票中投票。对于要显示的每个轮询,请检查该轮询是否在当前用户的轮询列表中。如果是,不要让他们投票。

And when the current user votes in a poll, add that poll to the current user's list of polls.

当当前用户在轮询中投票时,将该轮询添加到当前用户的轮询列表中。