是否有扩展 MongoDB 游标对象以使其与 Djangodjango.core.paginator.Paginator
类兼容的已知方法?
或者,也许,扩展 Django 的类?
是否有扩展 MongoDB 游标对象以使其与 Djangodjango.core.paginator.Paginator
类兼容的已知方法?
或者,也许,扩展 Django 的类?
您的临时解决方案(https://gist.github.com/2351079)看起来不错 - 但不是强制光标使用 获取所有结果list()
并使用 分页[bottom:top]
,而是尝试在光标上显式使用.skip()
和.limit()
- 它可能会提高性能。
我遇到了同样的问题并实现了我自己的 Paginator 类。这是代码:
from django.core.paginator import Paginator, Page
class MongoPaginator(Paginator):
"""
Custom subclass of Django's Paginator to work with Mongo cursors.
"""
def _get_page(self, *args, **kwargs):
"""
Returns an instance of a single page. Replaced with our custom
MongoPage class.
"""
return MongoPage(*args, **kwargs)
def page(self, number):
"""
Returns a Page object for the given 1-based page number.
Important difference to standard Paginator: Creates a clone of the
cursor so we can get multiple slices.
"""
number = self.validate_number(number)
bottom = (number - 1) * self.per_page
top = bottom + self.per_page
if top + self.orphans >= self.count:
top = self.count
return self._get_page(
self.object_list.clone()[bottom:top], number, self
)
class MongoPage(Page):
"""
Custom Page class for our MongoPaginator. Just makes sure the cursor is
directly converted to list so that we can use len(object_list).
"""
def __init__(self, object_list, number, paginator):
self.object_list = list(object_list)
self.number = number
self.paginator = paginator
主要变化是:
这是在视图中使用它的辅助函数:
def get_paginated_cursor(request, cursor, per_page=25, param='page'):
"""
Helper to deal with some standard pagination. Pass a request and a
cursor and it will return a paginated version of it.
"""
paginator = MongoPaginator(cursor, per_page)
page = request.GET.get(param, 1)
try:
cursor = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
cursor = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
cursor = paginator.page(paginator.num_pages)
return cursor
然后你可以这样做:
def some_view_function(request):
messages_cursor = db.messages.find({})
messages = get_paginated_cursor(request, messages_cursor)