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

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

问题描述

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

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.

当前代码=

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

然后在 .py 方面,我有:

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()

我是通过复制别人的代码来获得这些代码的,但它确实有效.我需要知道代码是什么样子才能使其与 ndb 一起使用.(我对模型和包含语句没问题,我只需要知道如何在 jinja2 模板中检索密钥以及如何在 post 函数中使用它.

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.

请提供实际代码应该是什么样子才能使 ndb 工作.

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

提前致谢.

推荐答案

使用 NDB 键是一个属性而不是一个方法.所以你 listtype.key().id() 应该是 listtype.key.id()

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天全站免登陆