部分更新 App Engine 实体 [英] Partly update App Engine entity

查看:31
本文介绍了部分更新 App Engine 实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 App Engine 构建同步引擎,当我从客户端接收数据时,我想存储一个对象,但我不在乎它是否已经存在.如果我总是在更新时从客户端发送所有属性,那么今天效果很好.但我想...

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...

  • 客户端不知道的一些内部属性在更新后仍然存在
  • 客户端只能发送更改的值
  • 避免在更新之前获取所有对象,因为可能只有很少的对象需要更新

我是否需要获取每个对象,然后只更新我想更改的值,然后再更新对象?或者是否可以在不获取实体的情况下部分更新实体?

推荐答案

不,您不能在没有先阅读对象的情况下更新它.当您用新数据覆盖"一个对象时,该对象的新版本将包含显式写入的数据.

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.

例如(使用 NDB 语法):

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

这篇关于部分更新 App Engine 实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆