Storing User Objects in Google App Engine

2019-07-08 05:02发布

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?

1条回答
虎瘦雄心在
2楼-- · 2019-07-08 06:02

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.
  """
查看更多
登录 后发表回答