如何在Python for循环中获取GAE ndb中当前记录的密钥? [英] How do I get the key for the current record in GAE ndb in a Python for loop?

查看:103
本文介绍了如何在Python for循环中获取GAE ndb中当前记录的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个网页,其中显示来自具有编辑链接的数据存储的记录列表。我想从db转换这个。到ndb。我是一名Python和GAE新手。



当前的代码=

 < tbody> 
{%为列表类型中的列表类型%}
< tr>
< td> {{listtype.ListTypeName}}< / td>
< td>< a href =/ listtypes / edit / {{listtype.key()。id()}}>编辑< / a>< / td>
< / tr>
{%endfor%}
< / tbody>

然后在.py端,我有:

  def post(self,listtype_id):
iden = int(listtype_id)
listtypes = db.get(db.Key.from_path('ListTypes' ,iden))
listtypes.ListTypeName = self.request.get('ListTypeName')
listtypes.put()

我通过复制别人的代码来达到这些目的,但它可以工作。我需要知道代码如何使它与ndb一起工作。 (我对模型和include语句都很满意,我只需要知道如何检索jinja2模板中的键以及如何在post函数中使用它。



请提供真实代码应该看起来如何使这个工作与ndb。



预先感谢。

listtype.key().id()应该是 listtype.key.id()


I currently have a web page that presents a list of records from a datastore with an edit link. I want to convert this from db. to ndb. I am a Python and GAE newbie.

The current code =

  <tbody>
    {% for listtype in listtypes %}
    <tr>
      <td> {{ listtype.ListTypeName }} </td>
      <td><a href ="/listtypes/edit/{{ listtype.key().id() }}">edit </a></td>
    </tr>
    {% endfor %}    
  </tbody>

Then on the .py side, I have:

def post(self, listtype_id):
    iden = int(listtype_id)
    listtypes = db.get(db.Key.from_path('ListTypes', iden))
    listtypes.ListTypeName = self.request.get('ListTypeName')
    listtypes.put()

I got to these by copying someone else's code bbut it works. I need to know what the code would look like to make it work with ndb. (I am ok with the model and the include statements, I just need to know how to retrieve the key in the jinja2 template and how to use it in the post function.

Please provide what the actuall code should look to make this work with ndb.

Thanks in advance.

解决方案

With NDB key is an attribute not a method. So you listtype.key().id() should be listtype.key.id()

这篇关于如何在Python for循环中获取GAE ndb中当前记录的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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