擅长:python、mysql、java
<p>我不相信有文档记录,但是对于现有的<code>ext.db</code>模型,可以使用<code>db.to_dict()</code>(请参见<a href="https://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/db/__init__.py#3947" rel="nofollow">here</a>)。</p>
<p>尽管使用<code>db.ReferenceProperty</code>和<code>db.DateTimeProperty</code>时要小心,因为它们在调用<code>json.dumps()</code>时会抛出错误。快速解决方案是自定义JSONEncoder:</p>
<pre><code>from datetime import datetime, date, time
from google.appengine.ext import db
import json
class JSONEncoder(json.JSONEncoder):
def default(self, o):
# If this is a key, you might want to grab the actual model.
if isinstance(o, db.Key):
o = db.get(o)
if isinstance(o, db.Model):
return db.to_dict(o)
elif isinstance(o, (datetime, date, time)):
return str(o) # Or whatever other date format you're OK with...
</code></pre>
<p>然后用这个编码:</p>
<pre><code>JSONEncoder().encode(YourModel.all().fetch())
</code></pre>