shane716 shane716 - 1 month ago 8
Python Question

Google App Engine - updating an entity

I'm using Google App Engine 1.8.3 - Python 2.7 and NDB API as my datastore

Suppose I have a list of group entities displayed on a html table.
And I want to click the name of an individual group to edit the group's information.
What should the key/id of this group be, which one should I use? Please take a look at below

Here's my Model:

class Group(ndb.Model):
name = ndb.StringProperty()
description = ndb.StringProperty()


In my html:

......
{% for group in groups %}
<tr>
<td>
<a href="/editGroup?id={{ ????????????? }}" {{ group.name}} </a>
</td>
<td>
{{ group.description}}
</td>
</tr>
{% endfor %}
......


In the ????? inside the
<a>
tag, what should I put? what should I pass it back to the server? group.key? group.key.id()? or I have to add the Property to Group model as a key property like
groupId = ndb.IntegerProperty()
?

I'm thinking about using the entity's key, but I couldn't find a way to display the key in the html from 'group', like
group.something_to_get_the_key()
.

I was able to get the numeric id using
group.key.id()
, but according to https://developers.google.com/appengine/docs/python/ndb/entities#numeric_keys
numeric ids might not be unique.

Thanks

Answer

In NDB, keys have a urlsafe() method which produces a string suitable for use in templates and passing to URLs:

{{ group.key.urlsafe }}

This is documented on the same page you already link to.