Skip to main content

Django Vote

django-vote is a simple Django app to conduct vote for django model.

This project was inspired by django-taggit

Ci codecov PyPI version

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 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
{% 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/

Metadata

Release files for django-vote 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-vote 2.5.0
File Size Uploaded
django_vote-2.5.0.tar.gz 20.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-vote 2.5.0
File Interpreter ABI Platform
django_vote-2.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.8 kB

Release files / django_vote-2.5.0.tar.gz

Download URL django_vote-2.5.0.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d15b1d315df97b10d3e9e0bd4897fd4650de8a1f8b8aa0563efc4779b40ec352
BLAKE2b-256 checksum
How to use checksums
618a0a28b15b69da4c00036b3582238524a663cb35d4edb297302e89c6b24edd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / django_vote-2.5.0-py3-none-any.whl

Download URL django_vote-2.5.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c1409f7edbd98ab05607b14ff86ae6fc41be05a06dcede54d7d55d95a192956
BLAKE2b-256 checksum
How to use checksums
f64e4cf3cc7d114a44669de2dcfd1a074917285ee23fcdc4de3005a3a64e8095
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

1 release file

2.1.7

1 release file

2.1.6

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.0.1

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1.1.0

1 release file

1.0.9

1.0.8

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page