How do I get the key for the current record in GAE ndb in a Python for loop?

放肆的年华 提交于 2020-02-03 16:24:11

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/11875903/how-do-i-get-the-key-for-the-current-record-in-gae-ndb-in-a-python-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!