webhelpers 1.0 paginate bugs

Bug 1:
在使用webhelpers 1.0 的paginate时碰到了
TypeError("Sorry, your collection type is not supported by the paginate module. "
            "You can either provide a list, a tuple, an SQLAlchemy 0.4 select object or an "
            "SQLAlchemy 0.4 ORM-query object.")
通过跟踪代码,获知是paginate.py 中 get_wrapper函数所出的问题,导致每次都执行至

View Code
raise TypeError("Sorry, your collection type is not supported by the "
"paginate module. You can provide a list, a tuple, a SQLAlchemy "
"select object or a SQLAlchemy ORM-query object.")
将get_wrapper函数修改成如下即可
View Code
def get_wrapper(obj, sqlalchemy_session=None):
"""
Auto-detect the kind of object and return a list/tuple
to access items from the collection.
"""
# If the collection is a sequence we can use it directly
if isinstance(obj, (list, tuple)):
return obj

# If object is iterable we can use it directly
if hasattr(obj, "__iter__") and hasattr(obj, "__len__"):
return obj

# Is SQLAlchemy 0.4 or better available? (0.3 is not supported - sorry)
if sqlalchemy_available[:3] != '0.3':
# Is the collection a query?
if isinstance(obj, sqlalchemy.orm.query.Query):
return _SQLAlchemyQuery(obj)
# Is the collection an SQLAlchemy select object?
if isinstance(obj, sqlalchemy.sql.expression.CompoundSelect) \
or isinstance(obj, sqlalchemy.sql.expression.Select):
return _SQLAlchemySelect(obj, sqlalchemy_session)

raise TypeError("Sorry, your collection type is not supported by the "
"paginate module. You can provide a list, a tuple, a SQLAlchemy "
"select object or a SQLAlchemy ORM-query object.")

Bug 2:
"__getitem__ without slicing not supported"异常究其原因是__init__函数中的
self.items = list(self.collection)[self.first_item-1:self.last_item]
所导致,当调用list(self.collection)时,会循环调用self.collection的__getitem__方法,其实也就是
_SQLAlchemyQuery类的__getitem__方法,此时__getitem__的输入参数类型为int,肯定不会是slice,按照设
计就导致raise Exception, "__getitem__ without slicing not supported",为解决这一问题,可以修改上述调用更改为:
View Code
#self.items = list(self.collection)[self.first_item-1:self.last_item]
self.items = list(self.collection[self.first_item-1:self.last_item])
原文地址:https://www.cnblogs.com/Jerryshome/p/2018775.html