votergroup = db.GqlQuery("SELECT * FROM Voter WHERE lastname = :1", 'AGEE')
for voter in votergroup:
voter.email = 'testemail@testemail.com'
db.put(votergroup)
The above code doesn't seem to be updating the records as it shows in the appengine documentation. I also tried using a query object to no avail. I know votergroup is pulling records, because I did a count on the object when debugging and it showed 10 records. In fact, before the db.put, I looped through voter.email, and it seems like the variable was set. However, the change never seems to make it back to the db.
Does anyone know what I might be doing wrong?
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.
来源:https://stackoverflow.com/questions/3553481/appengine-datastore-not-updating-multiple-records