Partly update App Engine entity

懵懂的女人 提交于 2020-01-03 05:55:14

问题


I'm building a sync engine with App Engine and when I receive data from the client I want to store an object but I don't care if it already exists or not. It works nice today if I always send all properties from the client when updating. But I want...

  • some internal properties not to be known by the client and still survive the update
  • the client to be able to only send the changed values
  • avoid fetching all objects before updating them as there can be quite few objects that need updates

Do I need to get each object and then update only the values I want to change and then update the object? Or is it possible to partly update entities without fetching them?


回答1:


No, you cannot update an object without first reading it. When you "overwrite" an object with new data, the new version of the object will contain only the data that was explicitly written.

You should probably make a list of properties that the client is allowed to set, and update the object (after reading it) with only those property values that the client sent and that are in the whitelist.

E.g. (using NDB syntax):

whitelist = ['prop1', 'prop2', ...]

def update_entity(key, **changed_values):
  ent = key.get()
  for name, value in changed_values.items():
    if name in whitelist:
      setattr(ent, name, value)  # Or ent._properties[name]._set_value(ent, value)
  ent.put()


来源:https://stackoverflow.com/questions/12078376/partly-update-app-engine-entity

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