shellfly / django-vote

Simple vote for django
http://django-vote.readthedocs.io
Apache License 2.0
158 stars 49 forks source link

How do I see how many up and down votes a model has? #69

Closed MysteryCoder456 closed 4 years ago

MysteryCoder456 commented 4 years ago

I also want to know how to check whether the user has up or downvoted.

shellfly commented 4 years ago

There are two fields called num_vote_up and num_vote_down on the VoteModel model that you can use when inheriting from it

shellfly commented 4 years ago

I also want to know how to check whether the user has up or downvoted.

For a single record

The simple way is documented on the README

# Check if the user already voted the object
review.votes.exists(user_id)

You can also pass a second optional parameter, check the code here: https://github.com/shellfly/django-vote/blob/f628a49c5e1e2ac2a91ac87922936e71d9b48523/vote/managers.py#L140-L145

For a list of records

If you have a list of objects that you want to attach is_vote_up and is_vote_down on them and don't want to query them one by one, you can use the add_field_to_objects helper function. https://github.com/shellfly/django-vote/blob/dc9d167e3aaf63d859b09917e0b8ebb594a4fdac/vote/utils.py#L17-L32

MysteryCoder456 commented 4 years ago

Ok thanks a lot!