Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shellfly/django-vote
Simple vote for django
https://github.com/shellfly/django-vote
django django-vote python vote
Last synced: 4 days ago
JSON representation
Simple vote for django
- Host: GitHub
- URL: https://github.com/shellfly/django-vote
- Owner: shellfly
- License: apache-2.0
- Created: 2014-07-07T08:51:42.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-06-20T16:03:03.000Z (7 months ago)
- Last Synced: 2025-01-18T23:29:07.834Z (11 days ago)
- Topics: django, django-vote, python, vote
- Language: Python
- Homepage: http://django-vote.readthedocs.io
- Size: 106 KB
- Stars: 159
- Watchers: 14
- Forks: 49
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Django Vote
``django-vote`` is a simple Django app to conduct vote for django model.
This project was inspired by [django-taggit](https://github.com/alex/django-taggit)
![Ci](https://github.com/shellfly/django-vote/actions/workflows/ci.yml/badge.svg)
[![codecov](https://codecov.io/gh/shellfly/django-vote/branch/master/graph/badge.svg)](https://codecov.io/gh/shellfly/django-vote)
[![PyPI version](https://badge.fury.io/py/django-vote.svg)](https://badge.fury.io/py/django-vote)### Quick start
#### Install `django-vote` by pip
```shell
pip install django-vote
```#### Add `'vote'` to your `INSTALLED_APPS` setting like this
```python
INSTALLED_APPS = (
...
'vote',
)
```#### Add `VoteModel` to the model you want to vote
```python
from vote.models import VoteModelclass ArticleReview(VoteModel, models.Model):
...
```#### Run migrate
```shell
manage.py makemigrations
manage.py migrate
```### Use vote API
```python
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 template tags
There are two template tags you can use in template:
1. `vote_count` to get vote count for a model instance
2. `vote_exists` to check whether current user vote for the instance``` html
{% load vote %}
{% for comment in comments %}
{{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"%}
{% endfor %}
```### Use `VoteMixin` for REST API
Install [django-rest-framework](https://github.com/encode/django-rest-framework/)
``` python
from rest_framework.viewsets import ModelViewSet
from vote.views import VoteMixinclass CommentViewSet(ModelViewSet, VoteMixin):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
``````sh
POST /api/comments/{id}/vote/
POST /api/comments/{id}/vote/ {"action":"down"}
DELETE /api/comments/{id}/vote/
```