google app engine ndb: put() and then query(), there is always one less item

前端 未结 5 1804
走了就别回头了
走了就别回头了 2021-01-11 20:36

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()<

5条回答
  •  旧巷少年郎
    2021-01-11 21:20

    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)
    

    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.

提交回复
热议问题