谷歌应用程序引擎ndb:放()然后查询(),总是有一个较少的项目 [英] google app engine ndb: put() and then query(), there is always one less item
问题描述
我想知道是否有人在Google App Engine NDB上遇到了一个奇怪的问题:在创建一个新实体并通过 put()
保存它之后,然后立即执行 query()
,总会有一个更少的项目。例如,
I wonder if anybody has encountered the strange problem on Google App Engine's NDB: after creating a new entity and saving it by put()
; and then query()
immediately, there is always one less item. For example,
class Item(ndb.Model):
...
...
items = Item.query().fetch()
length1 = len(items)
item = Item()
item.put()
items = Item.query().fetch()
length2 = len(items)
在上面, length1
总是等于 length2
。但是,稍后重新访问相同的HTML页面时, length2
将被更正。问题是什么?感谢。
In the above, length1
is always equal to length2
. However, length2
will be corrected when revisiting the same HTML page later. What is the problem? Thanks.
推荐答案
我通过进行适当的查询,创建新模型记录,然后在返回之前将其追加到我的查询它。修改你的,看起来像:
I solved this by making the appropriate query, creating the new model record, then appending it to my query before returning it. Modifying yours, it looks like:
class Item(ndb.Model):
...
...
items = Item.query().fetch()
length1 = len(items)
item = Item()
item.put()
appended_items = list()
for existing_item in items:
appended_items.append(existing_item)
appended_items.append(item)
length2 = len(appendeditems)
在这种情况下,attached_items包含您的查询,加上新元素。列表生成效率低下,但是我是一个python / ndb noob,可能有一种方法可以将Query Collection中的Collection取出来,这样会更好。
In this case, appended_items has your query, plus the new element. The list generation is inefficient, but I'm a python/ndb noob and there's probably a way to get the Collection right out of the Query model, which would be much better.
这篇关于谷歌应用程序引擎ndb:放()然后查询(),总是有一个较少的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!