I\'ve been searching for quite a while with no success. My project isn\'t using Django, is there a simple way to serialize App Engine models (google.appengine.ext.db.Model)
These APIs (google.appengine.ext.db) are no longer recommended. Apps that use these APIs can only run in the App Engine Python 2 runtime and will need to migrate to other APIs and services before migrating to the App Engine Python 3 runtime. To know more: click here
A simple recursive function can be used to convert an entity (and any referents) to a nested dictionary that can be passed to simplejson
:
import datetime
import time
SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
def to_dict(model):
output = {}
for key, prop in model.properties().iteritems():
value = getattr(model, key)
if value is None or isinstance(value, SIMPLE_TYPES):
output[key] = value
elif isinstance(value, datetime.date):
# Convert date/datetime to MILLISECONDS-since-epoch (JS "new Date()").
ms = time.mktime(value.utctimetuple()) * 1000
ms += getattr(value, 'microseconds', 0) / 1000
output[key] = int(ms)
elif isinstance(value, db.GeoPt):
output[key] = {'lat': value.lat, 'lon': value.lon}
elif isinstance(value, db.Model):
output[key] = to_dict(value)
else:
raise ValueError('cannot encode ' + repr(prop))
return output
Even if you are not using django as a framework, those libraries are still available for you to use.
from django.core import serializers
data = serializers.serialize("xml", Photo.objects.all())
If you use app-engine-patch it will automatically declare the _meta
attribute for you, and then you can use django.core.serializers
as you would normally do on django models (as in sledge's code).
App-engine-patch has some other cool features such has an hybrid authentication (django + google accounts), and the admin part of django works.
For simple cases, I like the approach advocated here at the end of the article:
# after obtaining a list of entities in some way, e.g.:
user = users.get_current_user().email().lower();
col = models.Entity.gql('WHERE user=:1',user).fetch(300, 0)
# ...you can make a json serialization of name/key pairs as follows:
json = simplejson.dumps(col, default=lambda o: {o.name :str(o.key())})
The article also contains, at the other end of the spectrum, a complex serializer class that enriches django's (and does require _meta
-- not sure why you're getting errors about _meta missing, perhaps the bug described here) with the ability to serialize computed properties / methods. Most of the time you serialization needs lay somewhere in between, and for those an introspective approach such as @David Wilson's may be preferable.
Mtgred's answer above worked wonderfully for me -- I slightly modified it so I could also get the key for the entry. Not as few lines of code, but it gives me the unique key:
class DictModel(db.Model):
def to_dict(self):
tempdict1 = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
tempdict2 = {'key':unicode(self.key())}
tempdict1.update(tempdict2)
return tempdict1