不确定你是否得到了你正在寻找的答案,但是你的意思是如何将Query对象中的模型(条目)数据直接解析为JSON对象? (至少这是我一直在寻找的)。
我写这从查询对象的条目解析成JSON对象的列表:
def gql_json_parser(query_obj):
result = []
for entry in query_obj:
result.append(dict([(p, unicode(getattr(entry, p))) for p in entry.properties()]))
return result
你可以有你的应用程序通过simplejson如编码它响应AJAX请求:
query_data = MyModel.all()
json_query_data = gql_json_parser(query_data)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(json_query_data))
您的应用将会返回如下内容:
[{'property1': 'value1', 'property2': 'value2'}, ...]
让我知道这是否有帮助!
来源
2011-09-29 08:30:55
xlm