Google App Engine获取()并打印 [英] Google App Engine fetch() and print

查看:91
本文介绍了Google App Engine获取()并打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型:

  class Rep(db.Model):
author = db.UserProperty( )
replist = db.ListProperty(str)
unique = db.ListProperty(str)
date = db.DateTimeProperty(auto_now_add = True)

我将 replist 写入数据存储区:

  L = [] 
rep = Rep()
s = self.request.get('sentence')
L.append(s)

rep.replist = L
rep.put()

并检索

  mylist = rep.all()。fetch(1)

我假设 mylist 是一个列表。我如何打印它的元素?当我尝试它时,我最终会看到物体;例如 [< __ main __。在0x04593C30的Rep对象>]



谢谢!



编辑



@Wooble:我也使用模板。我不明白的是,我列出了 L 这样的列表:

 % (len(L)):
< tr>
< td> $ {L [i]}< / td>
< / tr>
%endfor

这个工作。但是对于 mylist 也是如此。我试图用 T = type(mylist)得到 mylist 的类型,如果你使用 fetch(1),你会得到一个列表1个元素(或无,如果没有实体可以获取)。



通常,要在实体列表中打印每个实体的所有元素,您可以执行下列操作:

  props = Rep.properties()。keys()
for myentity in mylist:
for prop in props:
print%s: %s%(prop,getattr(myentity,prop))

尽管大多数人只会使用以某种方式显示实体数据的模板。


This is the model:

class Rep(db.Model):
    author = db.UserProperty()
    replist = db.ListProperty(str)
    unique = db.ListProperty(str)
    date = db.DateTimeProperty(auto_now_add=True)

I am writing replist to datastore:

        L = []
        rep = Rep()
        s = self.request.get('sentence')   
        L.append(s)

        rep.replist = L
        rep.put()

and retrieve

mylist = rep.all().fetch(1)

I assume that mylist is a list. How do I print its elements? When I try it I end up with the object; something like [<__main__.Rep object at 0x04593C30>]

Thanks!

EDIT

@Wooble: I use templates too. What I don't understand is that; I print the list L like this:

% for i in range(len(L)):
<tr>
  <td>${L[i]}</td>
</tr>
% endfor

And this works. But the same thing for mylist does not work. And I tried to get the type of mylist with T = type(mylist) that did not work either.

解决方案

If you use fetch(1), you'll get a list of 1 element (or None, if there are no entities to fetch).

Generally, to print all of the elements of each entity in a list of entities, you can do something like:

props = Rep.properties().keys()
for myentity in mylist:
     for prop in props:
         print "%s: %s" % (prop, getattr(myentity, prop))

Although most people would just be using a template to display the entities' data in some way.

这篇关于Google App Engine获取()并打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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