Say I have a model like this
class Foo(db.Model):
id = db.StringProperty()
bar = db.StringProperty()
baz = db.StringProperty()
And I'm going to GqlQuery like this
foos = db.GqlQuery("SELECT * FROM Foo")
I want to take the GqlQuery results and turn into some kind of JSON string that I can manipulate from different languages.
This is how i do it now
Add a method to the Foo class that converts it to a dictionary
def toDict(self):
return {
'id': self.id,
'bar': self.bar,
'baz': self'baz
}
Scroll through the GqlQuery results and manually add each instance of Foo to the dictionary
fooDict = {}
for foo in foos:
fooDict[foo.id] = foo.toDict()
return simplejson.dumps(fooDict)
My approach above works, but it feels rude.
Is there a cleaner, more βpythonicβ way to handle this?
The end format does not have to be exactly what I did above. It just needs to be something that translates well to JSON, so I can handle it from Javascript / PHP / whatever.