User from a user's ID

4 views
Skip to first unread message

wings

unread,
Nov 6, 2009, 9:32:33 PM11/6/09
to Google App Engine
Is there a good way to get a User object if you have their user ID?

Thanks !

Jeff S (Google)

unread,
Nov 9, 2009, 2:52:46 PM11/9/09
to Google App Engine
Hi David,

From looking through the code for the users API, it looks like you may
need the email to construct a User object from scratch:

http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/api/users.py

If the email address is None, the API attempts to determine the email
address by looking for a signed in user and it will only examine the
_user_id that you pass in after it looks at email. If you don't care
whether or not the email address is correct, you should be able to set
it to a fake value (like 'a'). If you could tell us a bit more about
how you'd like to use this we might be able to give more specific
advice ;-)

Thank you,

Jeff

wings

unread,
Nov 9, 2009, 10:23:55 PM11/9/09
to Google App Engine
I've been displaying a user's profile page at a url like this:

/profile/a_user's_unique_id

At the top of a profile page I display the user's nickname. And I use
their unique id to fetch items they've favorited.

I run into a problem, however, when this user wants to share their
profile page with another person. I can't fetch a User object from a
unique id, so I can't get a nickname for a profile page -- unless that
user happens to be logged in.

A simple solution to this is to create an instance of a
UserPreferences model the first time they favorite something (with a
key name set equal to the user's id), and save their nickname to the
datastore. Then I could retrieve their nickname from the datastore
whenever their profile page is accessed.

Is this the recommended way to handle a user's nickname and
preferences?

Ikai L (Google)

unread,
Nov 10, 2009, 6:19:49 PM11/10/09
to google-a...@googlegroups.com
David,

We don't have an API for retrieving a User object from the User's ID. One approach you can take to solving this problem is to create a Profile class that references the User ID in its Key field, using the Profile handler in conjunction with the Key to retrieve this object instead. This is probably the way you want to go. It has the added bonus of being extensible, allowing you to store additional information for each user.
--
Ikai Lan
Developer Programs Engineer, Google App Engine
Reply all
Reply to author
Forward
0 new messages