Simplejson cannot directly serialize dB. Model in appengine to JSON. I did this:
The model module provides the following extensions:
Def getter (func ):
If not func. _ name _. startswith ("Get _"):
Raise invalidmethodname ("method name must start with 'get _'")
Func. getter = true
Return func
Class Resource (userdict. dictmixin ):
Def exposed_attrs (Self ):
"Attribute names to be exposed as keys """
Return []
Def hidden_keys (Self ):
"Keys to hide from iteration. A key will still be accessible if it is
A getter or if it is listed in exposed_attrs ()"""
Return []
Def child_object (self, name ):
"Called to get a child object if it isn' t found as a getter or an attribute """
Raise attributeerror
# Dictionary Methods
Def _ getitem _ (self, key ):
"Called to implement evaluation of self [Key]""
Getter = getattr (self, 'Get _ '+ key, none)
If hasattr (getter, 'getter '):
Return getter ()
If key in self. exposed_attrs ():
Return getattr (self, key)
Return self. child_object (key)
Def _ setitem _ (self, key, value ):
"Called to implement assignment to self [Key]""
Setter = getattr (self, 'set _ '+ key, none)
If hasattr (setter, 'setter '):
Return setter (value)
Def _ delitem _ (self, key ):
"Called to implement deletion of self [Key]""
Deleter = getattr (self, 'del _ '+ key, none)
If hasattr (deleter, 'delete '):
Return deleter (value)
Def keys (Self ):
"" List of keys for iteration """
Getter_keys = [name. Replace ('get _ ', '', 1) for name in Dir (Self)
If hasattr (getattr (self, name, none), 'getter ')]
Exposed_keys = getter_keys + self. exposed_attrs ()
Return [key for key in exposed_keys if key not in self. hidden_keys ()]
When the database. model is inherited, the resource is also inherited:
Class personmodel (resource, DB. Model ):
User_id = dB. stringproperty ()
User_content = dB. textproperty ()
@ Getter
Def get_uid (Self ):
Return self. user_id
@ Getter
Def get_content (Self ):
Return self. user_content
When processing the request, do the following:
Class resthandler (webapp. requesthandler ):
Def post (Self ):
Self. dispatch_request ()
Def dispatch_request (Self ):
Self. response. headers ["Content-Type"] = "text/plain"
Params = {}
For key in self. Request. Params:
Params [STR (key)] = self. Request. Get (key)
OBJ = // fetch data from personmodel.
OBJ = self. preserialize (OBJ)
Out = simplejson. dumps (OBJ)
Self. response. Out. Write (out)
Def translate (self, OBJ, depth ):
Return OBJ
Def preserialize (self, OBJ, depth = 0 ):
OBJ = self. Translate (OBJ, depth)
If isinstance (OBJ, datetime. datetime ):
Return obj. isoformat ()
Elif isinstance (OBJ, DB. Users. User ):
Return obj. Nickname ()
Elif isinstance (OBJ, (db. geopt, DB. Im, DB. Key )):
Return STR (OBJ)
Elif isinstance (OBJ, types. listtype ):
Return [self. preserialize (item, depth + 1) for item in OBJ]
Elif isinstance (OBJ, (types. dicttype, resource )):
Copy = {}
For key in OBJ:
Copy [Key] = self. preserialize (OBJ [Key], depth + 1)
Return copy
Return OBJ
This method is not very good, and there is a lot of room for optimization...