问题
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