【问题标题】:Storing User Objects in Google App Engine在 Google App Engine 中存储用户对象
【发布时间】:2013-03-11 18:08:07
【问题描述】:

尝试找出将 Webapp2 Auth 用户对象作为参考存储在 google 应用引擎 ndb 域实体中时的最佳实践。

我能想到的三种方法

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

我不确定这里的最佳做法是什么?特别是存储 user_id vs key 有什么好处吗?假设 UserProperty 是旧的做事方式?

【问题讨论】:

    标签: python google-app-engine webapp2


    【解决方案1】:

    避免使用 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.
      """
    

    【讨论】:

    • 是不是id比key好,因为他们也有KeyProperty(允许指定特定类型)会不会想到存储key更好?
    • ID 是用户的唯一标识,Key 可以是特定于实现的。
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    相关资源
    最近更新 更多