Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine: Get entity key to use in a template

Assuming I have the following:

class Person(db.Model):
  name = db.StringProperty()

I would like to print all the names in an html file using a template.

template_values = {'list': Person.all()}

And the template will look like this:

{% for person in list %}
<form>
  <p>{{ person.name}} </p>
  <button type="button" name="**{{ person.id }}**">Delete!</button>
</form>
{% endfor %}

Ideally I would like to use person.key or person.id to then be able to delete the record using the key but that doesn't seem to work. Any Ideas how can I accomplish this?

like image 465
Luis Benavides Avatar asked Nov 22 '25 04:11

Luis Benavides


1 Answers

Use {{person.key.id}}, not just {{id}}. This will call each object's .key().id() method(s).

However, you should also be aware that passing Person.all() as a template value isn't necessarily a great idea; .all() returns a db.Query object, which can be treated as an iterable like you're doing but which will do multiple RPCs as you iterate through the query; instead you should use something like Person.all().fetch(SOME_NUMBER), where SOME_NUMBER is a reasonable amount to display to the user (or an arbitrarily large number if you insist on trying to display everything in one view.)

like image 92
Wooble Avatar answered Nov 23 '25 20:11

Wooble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!