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