this question我想出了下面的代码。
行为应如下:
Suggestion
可以收到多个Vote
Vote
它工作正常,但我想知道最好的方法是什么?
vote_form.html
<form action="{% url 'add_vote' suggestion.id %}" method="post">
{% csrf_token %}
<input type="submit" value="I want to vote">
</form>
urls.py
urlpatterns = [
url(r'^suggestion/(?P<pk>\d+)/$', views.SuggestionDetail.as_view(), name="suggestion_detail"),
url(r'^suggestion/(?P<pk>\d+)/vote/$', views.add_vote, name='add_vote'),
]
models.py
class Vote(models.Model):
suggestion = models.ForeignKey(Suggestion)
voter = models.ForeignKey('auth.User')
vote_count = models.BooleanField()
views.py
def add_vote(request, pk):
suggestion = get_object_or_404(Suggestion, pk=pk)
vote = Vote(
suggestion = suggestion,
voter = request.user,
vote_count = True)
has_user_voted = Vote.objects.filter(voter=request.user, suggestion=suggestion).count()
if has_user_voted < 1:
vote.save()
else:
messages.error(request, 'It seems you have already voted, only one vote is allowed')
return HttpResponseRedirect(reverse('suggestion_detail', args=(suggestion.id,)))
答案 0 :(得分:2)
第一个解决方案是在模型中添加unique-together
class Vote(models.Model):
suggestion = models.ForeignKey(Suggestion)
voter = models.ForeignKey('auth.User')
vote_count = models.BooleanField()
class Meta:
unique_together = ("suggestion", "voter")
如果vote_count
始终为True
,您也可以使用get-or-create
def add_vote(request, pk):
suggestion = get_object_or_404(Suggestion, pk=pk)
vote, created = Vote.objects.get_or_create(
voter=request.user,
suggestion=suggestion,
vote_count=True)
if not created:
messages.error(request, 'It seems you have already voted, only one vote is allowed')
return HttpResponseRedirect(reverse('suggestion_detail', args=(suggestion.id,)))