django-voting 与分页器相结合:“generator”类型的 TypeError 对象;没有 len()

发布于 2024-11-04 22:42:09 字数 2757 浏览 3 评论 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'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-11-11 22:42:24

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)

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)
皓月长歌 2024-11-11 22:42:23

尝试做:

comments_paginator = Paginator(list(comments_all), 20)

Try to do:

comments_paginator = Paginator(list(comments_all), 20)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文