0

更新

我要做的就是根据投票分数获取对象列表并将其发送到模板。如果您以前使用过 django-voting,请帮我解决这个问题。我需要一个列表,因为我将该列表传递给分页器应用程序。

我应该问一个不同的问题(也许关闭这个问题)?


我正在尝试按照它们的分数顺序获取所有对象,将它们附加到列表并将其发送到模板,但我遇到了错误。你能告诉我按投票分数的顺序快速检索我的对象吗?

get_top()方法来自 django-voting 应用程序管理器:链接 github 上的方法:

https://github.com/brosner/django-voting/blob/master/voting/managers.py#L122

我在我的视图中收到所有评论:

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)

错误跟踪:

    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()

更新:我试图做:

comments_all = list(Vote.objects.get_top(Comment, 100, False))

但它给出了错误:

int() argument must be a string or a number, not 'tuple'
4

2 回答 2

1

试着做:

comments_paginator = Paginator(list(comments_all), 20)
于 2011-05-05T11:50:48.867 回答
1

http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations-and-aggregation “Django 的数据库聚合 API 不适用于 GenericRelation。例如,您可能想尝试就像是”

您可以使用 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)
于 2011-05-08T18:15:32.943 回答