帮助理解with_cursor和查询

时间:2011-09-19 18:30:45

标签: python google-app-engine

我认为我理解了基于http://code.google.com/appengine/docs/python/datastore/queryclass.html的游标和查询的概念,但显然它不是基于我在代码中所做的事情。

我的数据存储区中有310个项目,我希望以100个小批量大小迭代它们:

query = Event.all()
query.order("__key__")

batch_size = 100;

# expecting this to return the 1st 100 of the 310 items    
results = query.fetch(limit=batch_size)

logging.info("count 1: %d, results: %d" % (query.count(), len(results)))
# reports: count 1: 310, results: 100

for item in results:
    print item # this will print items 1-100, which is expected

# Move to the next batch block
cursor = query.cursor();
query.with_cursor(cursor);            

results = query.fetch(limit=batch_size)
logging.info("count 2: %d, results: %d" % (query.count(), len(results)))
# reports: count 2: 0, results: 0 
# but was expecting to move to item 101

如何以100的批量迭代我的所有实体? 'query.cursor()'是否返回第一个batch_size末尾的光标或该块的开头?

1 个答案:

答案 0 :(得分:2)

.count()确实在挫败你。要查看原因,请创建第二个相同的查询,将保存的光标应用于该查询,并查看会发生什么。

顺便说一下,__key__顺序是隐含的。