在Google App Engine中存储用户对象 [英] Storing User Objects in Google App Engine

查看:152
本文介绍了在Google App Engine中存储用户对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试找出将Webapp2 Auth用户对象作为参考引入Google应用引擎ndb域实体的最佳做法。



我能想到的3种方法做到这一点

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

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

在那里,我将存储webapp2用户对象的用户标识,如

  user.get_id()

code>

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

其中我将从webapp2 User对象中存储用户密钥,如

  user.key 

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

解决方案

避免UserProperty,存储ID。



直接从源... ...

pre $ google $ appengine ext / ndb / model。 py:1711
$ b $ class UserProperty(Property):
一个属性,它的值是一个User对象

注意:这是为了向后兼容现有的
数据存储模式;我们不建议将用户对象
直接存储在数据存储中,而是建议存储
user.user_id()值。


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?

解决方案

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

这篇关于在Google App Engine中存储用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆