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

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

问题描述

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


  • 一些内部属性不被客户端知道,并且仍然保持更新状态。

  • 客户端只能发送已更改的值

  • 避免在更新之前获取所有对象,因为可能需要更新的对象很少



我是否需要获取每个对象,然后只更新要更改的值,然后更新该对象?还是有可能部分更新实体而不提取它们? 阅读它。当您用新数据覆盖一个对象时,对象的新版本将仅包含明确写入的数据。



您应该可以创建一个允许客户端设置的属性列表,并且只需要客户端发送并且位于白名单中的属性值就可以更新对象(阅读完毕后)。



例如(使用NDB语法):

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

def update_entity(key,** changed_values):
ent = key.get()
用于名称,值为changed_values.items():
如果名称在白名单中:
setattr(ent,name,value)#或ent._properties [name] ._ set_value(ent,value)
ent.put()


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?

解决方案

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

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

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