Search code examples
pythongoogle-app-enginedatamodel

How do I search for an entity in Google App engine by user?


I am attempting to lookup a user entity that is related to the current user, and I do not seem to be able to find the user after they get stored in the database.

When I am creating the user for the first time, the call I make looks like the following:

newPlayer = model.Player(parent=model.user_key(), user=users.GetCurrentUser(), publicName = nickname)
newPlayer.put()

Where model.Player is the element I am trying to lookup, and model.user_key() gets a global ancestor user key.

When I go to look them up, I do the following:

model.Player.all().filter('user =', usr).ancestor(user_key()).fetch(1)

The model Player class looks like this:

class Player(db.Model):
    user = db.UserProperty
    publicName = db.StringProperty()

This returns an empty list every time. What am I doing wrong here? I am starting to question determinism at this point...


Solution

  • User is not a class in datastore, so you can't make player a child of it. This should work:

    newPlayer = model.Player(user=users.get_current_user(), publicName='john')
    newPlayer.put()
    
    usr = users.get_current_user()
    john = model.Player.all().filter('user =', usr).fetch(1)
    print john.nickname
    >> 'john'
    

    Note: is get_current_user() not GetCurrentUser() source