代码之家  ›  专栏  ›  技术社区  ›  Zeynel

Google App Engine fetch()并打印

  •  0
  • Zeynel  · 技术社区  · 15 年前

    这是模型:

    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__.Rep object at 0x04593C30>]

    谢谢!

    @我也用模板。我不明白的是,我把名单打印出来了 L

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

    这很管用。但同样的事情 迈利斯特 迈利斯特 T = type(mylist) 那也不管用。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Wooble    15 年前

    如果你使用 fetch(1) ,将得到一个包含1个元素的列表(如果没有要获取的实体,则为无)。

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

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

        2
  •  0
  •   Luis Benavides    15 年前

    结果 rep.all().fetch(1) 是一个物体。你需要这样迭代:

    {% for i in mylist %}
    <tr>
      <td>{{i.author }}</td>
      ...
    </tr>
    {% endfor %}
    

    {% for i in mylist %}
      <tr>
        <td>{{i.replist|join:"</td><td>" }}</td>
      </tr>
    {% endfor %}