我想打印出一個查詢對象的結構,類似於您可以在交互式Python提示符處打印字典或列表的內容。我發現我很難想象數據結構是什麼樣子,這當然會讓模板參數傳遞更難。如何顯示GAE查詢對象的內容和結構?
例如,這裏是一個非常簡單的UserProfile db類,而我試圖打印出所有配置文件以及第二個配置文件的內容。
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
class UserProfile(db.Model):
user = db.UserProperty(auto_current_user_add=True)
name = db.StringProperty()
class TestHandlerBasic(webapp.RequestHandler):
def get(self):
profiles = UserProfile.all()
self.response.out.write('The whole profiles: ')
self.response.out.write(profiles)
self.response.out.write('<p>')
self.response.out.write('Now, print out all profiles: <p>')
for profile in profiles:
self.response.out.write(profile.user)
self.response.out.write('<br>')
self.response.out.write('<p>')
self.response.out.write('There are this many profiles: ')
self.response.out.write(profiles.count())
self.response.out.write('<p>This is the second profile profiles[1]: ')
self.response.out.write(profiles[1])
return
application = webapp.WSGIApplication([(r'/testbasic', TestHandlerBasic),] debug=True)
我得到這樣的輸出:
整體概況:
現在,打印出的所有模式:
[email protected]
ARD @例子。 com有這麼多配置文件:2
這是第二個輪廓曲線[1]:
第一線,我得到<google.appengine.ext.db.Query object at 0x490bd10>
最後一行我得到<models.UserProfile object at 0x490bb90>
後,也是如此。那麼,如何打印Query對象或模型對象的轉儲?
我正在通過克里斯的'BaseModel'參考,看看我可以如何合併到我所做的。另外,http://svn.assembla.com/svn/swewi_javaprograms/swenglish/swewi/common/appenginepatch/appenginepatcher/patch.py有一些猴子補丁和一個'__repr__'我需要翻閱的例子。 – 2011-03-23 22:35:03