22
33`` django-vote `` is a simple Django app to conduct vote for django model.
44
5- This project is inspired by [ django-taggit] ( https://github.com/alex/django-taggit )
5+ This project was inspired by [ django-taggit] ( https://github.com/alex/django-taggit )
66
77![ Ci] ( https://github.com/shellfly/django-vote/actions/workflows/ci.yml/badge.svg )
88[ ![ codecov] ( https://codecov.io/gh/shellfly/django-vote/branch/master/graph/badge.svg )] ( https://codecov.io/gh/shellfly/django-vote )
@@ -41,7 +41,7 @@ manage.py makemigrations
4141manage.py migrate
4242```
4343
44- #### Use vote API
44+ ### Use vote API
4545
4646``` python
4747review = ArticleReview.objects.get(pk = 1 )
@@ -65,6 +65,9 @@ review.votes.exists(user_id, action=DOWN)
6565# Returns the number of votes for the object
6666review.votes.count()
6767
68+ # Returns the number of down votes for the object
69+ review.votes.count(action = DOWN )
70+
6871# Returns a list of users who voted and their voting date
6972review.votes.user_ids()
7073
@@ -74,9 +77,32 @@ Review.votes.all(user_id)
7477
7578```
7679
77- #### Use ` VoteMixin ` for REST API
80+ ### Use tags template
81+
82+ There are two template tags you can use in template:
83+ 1 . ` vote_count ` to get vote count for a model instance
84+ 2 . ` vote_exists ` to check whether current user vote for the instance
85+
86+ ``` html
87+ {% load vote %}
88+ <ol >
89+ {% for comment in comments %}
90+ <li >
91+ {{comment.content}} - up:{% vote_count comment "up" %} - down: {% vote_count comment "down" %} - exists_up:
92+ {% vote_exists comment user "up" %} - exists_down: {% vote_exists comment user "down"%}
93+ </li >
94+ {% endfor %}
95+ </ol >
96+ ```
97+
98+ ### Use ` VoteMixin ` for REST API
99+
100+ Install [ django-rest-framework] ( https://github.com/encode/django-rest-framework/ )
78101
79102``` python
103+ from rest_framework.viewsets import ModelViewSet
104+ from vote.views import VoteMixin
105+
80106class CommentViewSet (ModelViewSet , VoteMixin ):
81107 queryset = Comment.objects.all()
82108 serializer_class = CommentSerializer
0 commit comments