A simple Django app to conduct vote.
Project description
Django Vote
django-vote
is a simple Django app to conduct vote for django model.
This project was inspired by django-taggit
Quick start
Install django-vote
by pip
pip install django-vote
Add 'vote'
to your INSTALLED_APPS
setting like this
INSTALLED_APPS = (
...
'vote',
)
Add VoteModel
to the model you want to vote
from vote.models import VoteModel
class ArticleReview(VoteModel, models.Model):
...
Run migrate
manage.py makemigrations
manage.py migrate
Use vote API
review = ArticleReview.objects.get(pk=1)
# Up vote to the object
review.votes.up(user_id)
# Down vote to the object
review.votes.down(user_id)
# Removes a vote from the object
review.votes.delete(user_id)
# Check if the user already voted (up) the object
review.votes.exists(user_id)
# Check if the user already voted (down) the object
# import UP, DOWN from vote.models
review.votes.exists(user_id, action=DOWN)
# Returns the number of votes for the object
review.votes.count()
# Returns the number of down votes for the object
review.votes.count(action=DOWN)
# Returns a list of users who voted and their voting date
review.votes.user_ids()
# Returns all instances voted by user
Review.votes.all(user_id)
Use tags template
There are two template tags you can use in template:
vote_count
to get vote count for a model instancevote_exists
to check whether current user vote for the instance
{% load vote %}
<ol>
{% for comment in comments %}
<li>
{{comment.content}} - up:{% vote_count comment "up" %} - down: {% vote_count comment "down" %} - exists_up:
{% vote_exists comment user "up" %} - exists_down: {% vote_exists comment user "down"%}
</li>
{% endfor %}
</ol>
Use VoteMixin
for REST API
Install django-rest-framework
from rest_framework.viewsets import ModelViewSet
from vote.views import VoteMixin
class CommentViewSet(ModelViewSet, VoteMixin):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
POST /api/comments/{id}/vote/
POST /api/comments/{id}/vote/ {"action":"down"}
DELETE /api/comments/{id}/vote/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-vote-2.4.0.tar.gz
(19.6 kB
view details)
Built Distribution
File details
Details for the file django-vote-2.4.0.tar.gz
.
File metadata
- Download URL: django-vote-2.4.0.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dab49d59c509ee5193a84ecc49b3f61fa831ceb099ea4f8301a63b7368132426 |
|
MD5 | 1af120b4bc40d7f8b9dac66f719fd65a |
|
BLAKE2b-256 | 534d5b395451598500873a736c80ba53e9b8fa451679c5a2ea89c627a7c26626 |
File details
Details for the file django_vote-2.4.0-py3-none-any.whl
.
File metadata
- Download URL: django_vote-2.4.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffc468ef04c43a25cf2a1c890a0bf15c9eb15f9b16d8e55063068eb8510cb380 |
|
MD5 | 816a13c4490c5fd3db6568ccf9a96370 |
|
BLAKE2b-256 | 3aa3df9dad67ab579a26369324b2334ada9bb16243f6c909d90df6d4d5242f64 |