我执行app引擎查询以获取游标(wrec),代码通过迭代正确显示记录数。但是“for rec in wrec”不会运行(此循环中没有logging.info)。
还有一个同一个表的GQL SELECT,以及jinja2正确呈现的另一个游标(wikiCursor)。这是不起作用的部分:
wrec = Wiki.all().ancestor(wiki_key()).filter('pagename >=', findPage).filter('pagename <', findPage + u'\ufffd').run()
foundRecs = sum(1 for _ in wrec)
logging.info("Class WikiPage: foundRecs is %s", foundRecs)
aFoundRecs = []
if foundRecs > 0:
for rec in wrec:
logging.info("Class WikiPage: value is %s", wrec.pagename)
aFoundRecs.append(rec.pagename)
self.render("permalink.html", userRec=self.userRec, wikipage=pagename,
wikiCursor=wikiCursor, wrec=wrec, foundRecs=foundRecs)
else:
errorText = "Could not find anything for your entry."
self.render("permalink.html", userRec=self.userRec, wikipage=pagename, wikiCursor=wikiCursor, error=errorText)
以下是日志的一部分,显示了第一个logging.info语句,但不显示第二个:
INFO 2014-09-15 19:35:29,525 main.py:410] Class WikiPage: foundRecs is 3
INFO 2014-09-15 19:35:29,581 module.py:652] default: "POST / HTTP/1.1" 200 3058
为什么wrec for loop没有运行?
答案 0 :(得分:1)
当您使用sum计数时,查询已经迭代直到结束。这是预期的行为,如果你试图再次迭代它,它不会工作(因为它已经在最后)