Search code examples
pythongoogle-app-enginewebapp2

Storing User Objects in Google App Engine


Trying to figure out the best practice when storing Webapp2 Auth user objects as a reference in a google app engine ndb domain entity.

The 3 ways I can think to do it

class MyEntity(ndb.Model):
  users = ndb.UserProperty(repeated=True)

or

class MyEntity(ndb.Model):
  users = ndb.StringProperty(repeated=True)

where I would store user id's from the webapp2 User object like

user.get_id()

or

class MyEntity(ndb.Model):
  users = ndb.KeyProperty(repeated=True)

where I would store user key from the webapp2 User object like

user.key

I am not sure what is the best practice here? In particular is there any advantage to storing user_id vs key? Assuming UserProperty is the old way of doing things?


Solution

  • Avoid UserProperty, store the ID instead.

    Straight from the source...

    # google/appengine/ext/ndb/model.py:1711
    
    class UserProperty(Property):
      """A Property whose value is a User object.
    
      Note: this exists for backwards compatibility with existing
      datastore schemas only; we do not recommend storing User objects
      directly in the datastore, but instead recommend storing the
      user.user_id() value.
      """