在Google App Engine中存储用户对象

2 投票
1 回答
1053 浏览
提问于 2025-04-17 18:43

我正在想如何在谷歌应用引擎的ndb域实体中,存储Webapp2认证用户对象的最佳方法。

我想到的三种方法是:

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

或者

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

在这里,我会存储来自webapp2用户对象的用户ID,比如:

user.get_id()

或者

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

在这里,我会存储来自webapp2用户对象的用户键,比如:

user.key

我不太确定这里的最佳实践是什么?特别是存储用户ID和用户键有什么优缺点?假设UserProperty是以前的做法?

1 个回答

3

避免使用UserProperty,直接存储ID。

直接来自源头...

# 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.
  """

撰写回答