如何显示GAE查询对象的内容和结构? [英] How to display GAE Query object contents and structure?

查看:94
本文介绍了如何显示GAE查询对象的内容和结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印Query对象的结构,类似于如何在交互式Python提示符处打印字典或列表的内容。我发现我很难想象数据结构是什么样子,这当然会让模板参数传递更难。



例如,这里是一个很简单的UserProfile数据库类,我试图打印出所有配置文件和第二个配置文件的内容。

  from google.appengine .ext从google.appengine.ext导入db 
从google.appengine.api导入webapp
从google.appengine.ext.webapp.util导入用户
导入run_wsgi_app

class UserProfile(db.Model):
user = db.UserProperty(auto_current_user_add = True)
name = db.StringProperty()
$ b $ class TestHandlerBasic(webapp.RequestHandler) :
def get(self):
profiles = UserProfile.all()
self.response.out.write('整个配置文件:')
self.response.out .write(profiles)
self.response.out.write('< p>)
self.response.out.write('Now,print o所有配置文件:< p>)
用于配置文件中的配置文件:
self.response.out.write(profile.user)
self.response.out.write('< ('有< p>')
self.response.out.write('< p>)
self.response.out.write('有这么多配置文件:')
self。 response.out.write(profiles.count())
self.response.out.write('< p>这是第二个配置文件[1]:')
self.response.out .write(profiles [1])$ ​​b $ b return

application = webapp.WSGIApplication([(r'/ testbasic',TestHandlerBasic),] debug = True)

我得到这样的输出:


整个配置文件:



现在,打印出所有配置文件:

test@example.com

ard@example.com



有很多配置文件:2



这是第二个个人资料配置文件[1]:


在第一行之后,我得到< google.appengine.ext.db.Query对象在0x490bd10> 和last我也得到< models.UserProfile object at 0x490bb90> 。那么,如何打印Query对象或模型对象的转储?

解决方案

查询对象本身并不存在它们由 get() fetch()返回的db.Model实体。因此,您必须切换 UserProfile.all(),例如:

  profiles = UserProfile.all()。fetch(1000)

我个人喜欢将子类db.Model类,并为序列化我的模型添加一些便利。我主要是这样做的,所以我可以快速获得实体的JSON表示,但是它也非常方便的进行倾倒/检查。



以下是我的 BaseModel 扩展,它添加了 __ dict __ __ json __ code>方法到实体。如果您还添加了转储 __ dict __ 方法的 __ repr __ ,则可以在打印实体时改善实体的字符串表示到控制台。


I want to print out the structure of a Query object, similar to how you can print out the contents of a dict or list at the interactive python prompt. I've found I'm having trouble visualizing what the data structures look like, which of course makes passing in template arguments much harder.

For example, here is a very simple UserProfile db class, and me attempting to print out all profiles, and the contents of the second profile.

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)

I get this kind of output:

The whole profiles:

Now, print out all profiles:

test@example.com
ard@example.com

There are this many profiles: 2

This is the second profile profiles[1]:

After the first line I get <google.appengine.ext.db.Query object at 0x490bd10> and the last line I get <models.UserProfile object at 0x490bb90>, as well. So, how do I print out a dump of the Query object or models object?

解决方案

Query objects don't themselves hold any db.Model entities they are returned by get() or fetch(). So you would have to to switch UserProfile.all() for something like:

profiles = UserProfile.all().fetch(1000)

Personally I like to subclass the db.Model class and add a few conveniences for serialising my models. I do this mainly so I can get quick JSON representations of entities, but it's quite handy for dumping/inspecting them too.

Here's an example of my BaseModel extension that adds __dict__ and __json__ methods to entities. If you also added an __repr__ that dumps the __dict__ method you could improve the string representation of the entities when they are printed to the console.

这篇关于如何显示GAE查询对象的内容和结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆