如何用Query替换gql类型的查询?

LA_*_*LA_ 1 google-app-engine google-cloud-datastore

我有以下代码,效果很好:

comments = PersonComment.gql('WHERE ANCESTOR IS :parent AND verified=True ORDER BY added DESC', parent=person_key).fetch(PAGE_SIZE_COMMENTS+1, (page)*PAGE_SIZE_COMMENTS)
Run Code Online (Sandbox Code Playgroud)

我想用以下内容替换:

  comments = db.Query(PersonComment)
  comments.ancestor(person_key)
  comments.filter('verified = ', True)
  comments.order('-added')
  comments.fetch(PAGE_SIZE_COMMENTS+1, (page)*PAGE_SIZE_COMMENTS)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.有什么问题?

Dre*_*ars 5

fetch返回结果集; comments仍然是一个Query对象.你可以这样做:

comments = comments.fetch(PAGE_SIZE_COMMENTS+1, (page)*PAGE_SIZE_COMMENTS)

或者将您的查询对象称为其他内容以避免混淆.

另外,使用带偏移的fetch进行分页实际上效率很低.考虑使用查询游标.