I am working on a small university project. And I want to add voting to my app.
I've decided to use django-vote for it.
Here is the documentation: https://pypi.org/project/django-vote/
Upvoting works fine.
The problem is whenever I want to delete existing vote it doesn't work.
I saw this thread Django model: delete() not triggered
but I didn't understand it.
from vote.models import UP, DOWN
...
book = get_object_or_404(Book, id=pk)
...
if 'upvote' in request.POST:
print("I clicked upvote")
if book.votes.exists(request.user.id):
print("upvote exists")
book.votes.delete(request.user.id)
else:
book.votes.up(request.user.id)
if 'downvote' in request.POST:
print("I clicked downvote")
if book.votes.exists(request.user.id, action=DOWN):
print("downvote exists")
book.votes.delete(request.user.id)
else:
book.votes.down(request.user.id)
My model:
class Book(VoteModel, models.Model):
....