The API

After you’ve got your VoteModel added to your model you can start playing around with the API.

class VotableManager([through=None, verbose_name="Votes", field_name='votes', extra_field=None])
Parameters:
  • verbose_name – The verbose_name for this field.
  • through – The through model
  • field_name – The field name added to the query
  • extra_field – The field on your model. It will be updated when up or down
up(user_id)

This adds a vote to an object by the user. IntegrityError will be raised if the user has voted before:

>>> comments.votes.up(user)
down(user_id)

Removes the vote from an object. No exception is raised if the user doesn’t have voted the object.

delete(user_id)

Removes the vote from an object. No exception is raised if the user doesn’t have voted the object.

exists(user_id, action=UP)

Check if user has voted the instance before.

all(user_id, action=UP)

Get all instances voted by the specify user.

user_ids(action=UP)

Get all user_ids voted the instance

count(action=UP)

The count of all votes for an object.

annotate(queryset=None, user_id=None, reverse=True, sort=True)

Add annotation data to the queyset

Aggregation

Django does not support aggregation with GenericRelation currently but you still can use annotate:

>>> Comment.objects.filter(article__id=article_id).annotate(num_votes=Count('votes__user'))