Appengine datastore not updating multiple records

核能气质少年 提交于 2019-12-07 03:51:27

You need to call fetch() on the query you create with db.Query() to have it return a list of entities. You can then call put(list_of_entities) to persist them all. That looks like this:

voters = db.GqlQuery("SELECT * FROM Voter WHERE lastname = :1", 'AGEE').fetch(10)

for voter in voters:
    voter.email = 'testemail@testemail.com'
db.put(voters)

If you don't call fetch() on the query, you can still iterate over the results, and a datastore RPC will be made to retrieve small batches as they are needed. Calling put() on the query doesn't do anything, but you can still perform actions on each entity inside the loop.

voters_query = db.GqlQuery("SELECT * FROM Voter WHERE lastname = :1", 'AGEE')

for voter in voters_query:
    voter.email = 'testemail@testemail.com'
    voter.put()

Note that this does one datastore calls for each entity, plus one call for each batch being iterated over. It's much better to use fetch() unless you don't know how many items will be returned.

You can use cursors to break fetches up into larger chunks. I believe, though I can't find any proof, that fetch() has a limit of 1000 entities.

Try this instead:

votergroup = db.GqlQuery("SELECT * FROM Voter WHERE lastname = :1", 'AGEE')

for voter in votergroup:
    voter.email = 'testemail@testemail.com'
    voter.put()

I don't think there is a way to do mass edits with the app engine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!