JSON serialization of Google App Engine models

后端 未结 14 2106
不知归路
不知归路 2020-12-04 04:59

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)

14条回答
  •  抹茶落季
    2020-12-04 05:42

    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.

提交回复
热议问题