将谷歌应用引擎实体(在 python 中)复制到字典对象的好方法是什么?我正在使用 db.Expando 对象。所有属性都是扩展属性。
谢谢!
将谷歌应用引擎实体(在 python 中)复制到字典对象的好方法是什么?我正在使用 db.Expando 对象。所有属性都是扩展属性。
谢谢!
拥有一个名为foo
try 的实体:
foo.__dict__
试试这个。其中“m”是您希望转换为字典的 Expando 实例。
dict([(x,getattr(m,x)) for x in m.dynamic_properties()])
这应该工作
from google.appengine.ext import db
db.to_dict(entity)
新版本的 Google Cloud Python 客户端库运行起来并不那么优雅。所以这是一个快速修复。
your_dict = {x: entity[x] for x in entity.keys()}
请记住,字符串是作为 unicode 传入和返回的,而不是 basestring。;)
接受的答案应该是:
{}.update(entity}