使用查询迭代器在python中查询app引擎的游标

时间:2014-01-14 16:33:18

标签: python google-app-engine app-engine-ndb

我想使用分页而不是通过调用,

greets, next_curs, more = Greeting.query().fetch_page(10, start_cursor=curs)

但是使用查询迭代器。因为这样我可以使用tasklet将所有连续的俱乐部聚集在一起'获取'如https://developers.google.com/appengine/docs/python/ndb/async#tasklets

所述

现在我的代码看起来像,

class Book(ndb.Model):
    author = ndb.KeyProperty('a', kind=User, required=True)

author_key = ndb.Key(urlsafe=user_id)
books_query=Book.query(Book.author == author_key)
@ndb.tasklet
def callback(book):
     author = yield book.author.get_async()
     raise ndb.Return(book, author)

q_option = {'limit' : 2, 'produce_cursors' : True }
outputs = books_query.map(callback, **q_option)

不确定哪里出了问题但是如果我尝试做的话,

books_query.iter().cursor_before() 

books_query.iter().cursor_after()

以下跟踪失败,

raise datastore_errors.BadArgumentError('There is no cursor currently')
BadArgumentError: There is no cursor currently

使用带有游标的查询迭代器和tasklet的任何精心设计的示例都非常有助于我们解决这个问题。

1 个答案:

答案 0 :(得分:1)

books_query.iter().cursor_after()依赖.iter(**q_options),它会创建一个具有自己参数的QueryIterator。

问题是无法访问.map()使用的内部迭代器。

请参阅Guido对该主题相关问题的回答:https://stackoverflow.com/a/14150081/2380615

相关问题