6

将谷歌应用引擎实体(在 python 中)复制到字典对象的好方法是什么?我正在使用 db.Expando 对象。所有属性都是扩展属性。

谢谢!

4

5 回答 5

4

拥有一个名为footry 的实体:

foo.__dict__
于 2011-05-10T18:50:47.693 回答
2

试试这个。其中“m”是您希望转换为字典的 Expando 实例。

dict([(x,getattr(m,x)) for x in m.dynamic_properties()])
于 2011-05-10T21:10:43.347 回答
2

这应该工作

from google.appengine.ext import db
db.to_dict(entity)
于 2019-12-18T11:54:04.870 回答
0

新版本的 Google Cloud Python 客户端库运行起来并不那么优雅。所以这是一个快速修复。

your_dict = {x: entity[x] for x in entity.keys()}

请记住,字符串是作为 unicode 传入和返回的,而不是 basestring。;)

于 2018-08-02T17:01:34.527 回答
-2

接受的答案应该是:

{}.update(entity}
于 2017-11-27T15:26:46.893 回答