Chris Dutrow
Chris Dutrow

Reputation: 50362

App Engine Entity to Dictionary

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!

Upvotes: 6

Views: 3044

Answers (5)

shilpi
shilpi

Reputation: 21

This should work

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

Upvotes: 2

Dovy
Dovy

Reputation: 1276

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

Upvotes: 0

Camille Tolsa
Camille Tolsa

Reputation: 261

The accepted answer should be:

{}.update(entity}

Upvotes: -3

Tom Willis
Tom Willis

Reputation: 5303

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

Upvotes: 2

Aater Suleman
Aater Suleman

Reputation: 2328

Having an entity called foo try with:

foo.__dict__

Upvotes: 4

Related Questions