0

I'm am moving to using ndb now that it's out of the experimental phase. There are a few things that I need to rethink and one thing I am having trouble with is grabbing a unique identifier for a model entity client side. I grab entities as follows and send them to client side

questions = Questions.query()
self.values_for_client()["questions"] = questions

Then client side I iterate through each entry and then I set up a link for further investigating the model entry.

{% for question in questions %}
    <tr>
        <td><a href = "/view_details/{{question.key}}">View</a></td>
    </tr>
{% endfor %}

I used to get a unique identifier like asdfer234234=== but now I get something like Key('Questions',%203) from {{question.key}}. I would then capture this unique identifier and use it like

question_key = Key(str(cgi.escape(key)))
question = Questions.all().filter("__key__ =", question_key).get()

How can I replicate this behaviour with ndb?

user714852
  • 2,054
  • 4
  • 30
  • 52

1 Answers1

2

You want question.key.urlsafe()

https://developers.google.com/appengine/docs/python/ndb/keyclass#Key_urlsafe

lecstor
  • 5,619
  • 21
  • 27
  • Awesome - thanks for the quick response. As a follow up the actual implementation that worked was {{question.key.urlsafe}} i.e. no following () – user714852 May 30 '12 at 21:42
  • ah, yeh, that's the template system taking care of that for you. – lecstor May 30 '12 at 23:13
  • 1
    also, fyi, to turn that id back into a real key you can use ndb.Key(urlsafe=id) in your code.. – lecstor May 30 '12 at 23:14
  • 1
    Finally, have a look at the db/ndb conversion guide, where you could have found this information: https://docs.google.com/a/google.com/document/d/1AefylbadN456_Z7BZOpZEXDq8cR8LYu7QgI7bt5V0Iw/edit (a.k.a. NDB Cheat Sheet) – Guido van Rossum May 31 '12 at 15:49