webhelpers 1.0 paginate bugs

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.