HenryM
HenryM

Reputation: 5793

Retrieving GAE data via Key

I'm new to python & google app engine so this is going to be a basic question but I cannot find an answer elsewhere ...

I'm trying to retrieve a record by passing the key (as generated by GAE) from an HTML page back to python.

My html call is

<a href="/playerprofile?uid=ahRkZXZ-Y293YnJpZGdlYm9va2luZ3IWCxIGTWVtYmVyIgpKb2huIEpvbmVzDA">John Jones</a>

I'm then trying to pull it out of the table like this

uid = self.request.get('uid')
m = Member.all().filter('Key = ',uid)

It doesn't retrieve anything. How do I do it?

Upvotes: 1

Views: 66

Answers (2)

Tim Hoffman
Tim Hoffman

Reputation: 12986

You can't do it like this. The key is encoded in a urlsafe form

You should construct the Key from the encoded key using Key(encoded=None)

k = Key(uid)
m = Member.get(k)

Docs on Key https://developers.google.com/appengine/docs/python/datastore/keyclass

However given you are new to appengine you should not be using db. You should start using ndb as the datastore api, as that is where the development investment from google is going.

If you use ndb for this scenario you would use the following

m = Key(urlsafe=uid).get()

Upvotes: 2

Chris Bunch
Chris Bunch

Reputation: 89823

I would use Model.get_by_key_name() instead:

uid = self.request.get('uid')
m = Member.get_by_key_name(uid)

Here's more info about it on the App Engine documentation.

Upvotes: 0

Related Questions