App Engine Entity to Dictionary

柔情痞子 提交于 2020-01-23 06:53:33

问题


What is a good way to copy a google app engine entity (in python) to a dictionary object? I'm using db.Expando objects. All properties are expando properties.

Thanks!


回答1:


Having an entity called foo try with:

foo.__dict__



回答2:


try this. Where "m" is the instance of the Expando you wish to turn into a dictionary.

dict([(x,getattr(m,x)) for x in m.dynamic_properties()])



回答3:


This should work

from google.appengine.ext import db
db.to_dict(entity)



回答4:


The new version of the Google Cloud Python client library doesn't work quite so gracefully. So this is a quick fix.

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

Remember, the strings are passed in and returned as unicode, not basestring. ;)




回答5:


The accepted answer should be:

{}.update(entity}


来源:https://stackoverflow.com/questions/5954846/app-engine-entity-to-dictionary

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