UPDATE:
All I am trying to do is get the list of objects based on their voting score and send it to the template. If you have used django-voting before please help me figure this one out. I need a list because I pass that list to the paginator app.
Should I ask a different question (and maybe close this one)?
I am trying to get all the objects in order of the score they have, append them to a list and send it across to the template but I am getting errors. Can you please show me a quick to retrieve my objects in order of their voting score?
get_top()
method is from django-voting app managers: link the method on github:
https://github.com/brosner/django-voting/blob/master/voting/managers.py#L122
I am getting all comments in my View as:
comments_all = Vote.objects.get_top(Comment, 100, False)
"""URL param is latest, then sort by datetime
else sort by vote count, default"""
if sort == 'latest':
comments_all = Comment.objects.filter(book=book_id, active=True)\
.order_by('-created_datetime', '-modified_datetime')
else:
comments_all = Vote.objects.get_top(Comment, 100, False)
"""Pagination: if there is no page, set it to 1"""
comments_paginator = Paginator(comments_all, 20)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
#pagination: get the pages.
try:
comments_page = comments_paginator.page(page)
except (EmptyPage, InvalidPage):
comments_page = comments_paginator.page(comments_paginator.num_pages)
Error Trace:
Traceback:
File "/Users/AJ/work/projects/virtual_environments/bottledink/lib/python2.6/site- packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/AJ/work/projects/bottledink/../bottledink/main/views.py" in show_book
74. comments_page = comments_paginator.page(page)
File "/Users/AJ/work/projects/virtual_environments/bottledink/lib/python2.6/site-packages/django/core/paginator.py" in page
37. number = self.validate_number(number)
File "/Users/AJ/work/projects/virtual_environments/bottledink/lib/python2.6/site-packages/django/core/paginator.py" in validate_number
28. if number > self.num_pages:
File "/Users/AJ/work/projects/virtual_environments/bottledink/lib/python2.6/site-packages/django/core/paginator.py" in _get_num_pages
60. if self.count == 0 and not self.allow_empty_first_page:
File "/Users/AJ/work/projects/virtual_environments/bottledink/lib/python2.6/site-packages/django/core/paginator.py" in _get_count
53. self._count = len(self.object_list)
Exception Value: object of type 'generator' has no len()
UPDATE: I tried to do:
comments_all = list(Vote.objects.get_top(Comment, 100, False))
but it gives error:
int() argument must be a string or a number, not 'tuple'
http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations-and-aggregation "Django's database aggregation API doesn't work with a GenericRelation. For example, you might be tempted to try something like"
You can use https://github.com/coleifer/django-generic-aggregation
from django.contrib.comments.models import Comment
from django.db.models import Sum
from generic_aggregation import generic_annotate
from voting.models import Vote
#want might to filter on is_public and is_removed
top = generic_annotate(Comment.objects.all(), Vote.object, Sum('vote')).filter(active=True)