django pagination and RawQuerySet

谁说胖子不能爱 提交于 2019-11-30 08:50:24
Chris

I managed to achieve it using the following:

paginator = Paginator(files, 12)
paginator._count = len(list(files))

The code in django.core.paginator.py:

  • checks for whether _count is set
  • if not then tries to run .count() which doesn't exist
  • if not then tries plain len

len on a raw_queryset doesn't work but converting the actual paginator object to a list works find for me in Django 1.3

You can set the attribute count manually for your RawQuerySet object:

items = Item.objects.raw("select * from appitem_item")

def items_count():
    cursor = connection.cursor()
    cursor.execute("select count(*) from appitem_item")
    row = cursor.fetchone()
    return row[0]

items.count = items_count

for @Rockallite

>>> class A():
...    def b(self):
...        print 'from b'
... 
>>> 
>>> (A()).b()
from b
>>> def c():
...    print 'from c'
... 
>>> a = A()
>>> a.b = c
>>> a.b()
from c
qs.filter(**pfilter).distinct().extra(select={'test': 'COALESCE(`psearch_program`.`eu_price`, 999999999)'}).extra(order_by=['test'])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!