Let's say I have a model like this
class Foo(db.Model):
id = db.StringProperty()
bar = db.StringProperty()
baz = db.StringProperty()
And I'm going a GqlQuery like this
foos = db.GqlQuery("SELECT * FROM Foo")
I want to take the results of the GqlQuery and turn into some sort of JSON string that I can manipulate from different languages.
Here's how I'm doing it now
Add a method to the Foo class that converts it into a dictionary
def toDict(self): return { 'id': self.id, 'bar': self.bar, 'baz': self'baz }
Loop through the GqlQuery results and manually add each Foo instance to a dictionary
fooDict = {} for foo in foos: fooDict[foo.id] = foo.toDict() return simplejson.dumps(fooDict)
My approach above works but it feels kind of gross.
Is there a cleaner, more "Pythonic" way to handle this?
The end format doesn't have to be exactly what I've done above. It just has to be something that converts nicely to JSON so I can deal with it from Javascript/PHP/whatever.