[英]auto field values in ndb.StructuredProperty
我想将位置存储在Google的数据存储区中。 每个条目应具有“ sys”字段,其中应包含数据存储区设置的信息。 我有下面的类模型,WebService JSON请求/响应看起来还可以,但是我必须手动设置值。 看起来好像不会触发auto_current_user_add
, auto_now_add
, auto_current_user
和auto_now
。
from google.appengine.ext import ndb
from endpoints_proto_datastore.ndb import EndpointsModel
class Created(EndpointsModel):
by = ndb.UserProperty(auto_current_user_add=True)
on = ndb.DateTimeProperty(auto_now_add=True)
class Updated(EndpointsModel):
by = ndb.UserProperty(auto_current_user=True)
on = ndb.DateTimeProperty(auto_now=True)
class Sys(EndpointsModel):
created = ndb.StructuredProperty(Created)
updated = ndb.StructuredProperty(Updated)
class Location(EndpointsModel):
name = ndb.StringProperty(required=True)
description = ndb.TextProperty()
address = ndb.StringProperty()
sys = ndb.StructuredProperty(Sys)
当我提交创建请求( location.put()
)时,得到以下响应:
{
"id": "4020001",
"name": "asdf"
}
当我使用以下方法手动设置时:
location.sys = Sys(created=Created(on=datetime.datetime.now(),
by=current_user),
updated=Updated(on=datetime.datetime.now(),
by=current_user))
location.put()
我得到了预期的结果:
{
"id": "4020002",
"name": "asdf",
"sys": {
"created": {
"by": {
"auth_domain": "gmail.com",
"email": "decurgia@XYZ"
},
"on": "2015-01-27T16:05:41.465497"
},
"updated": {
"by": {
"auth_domain": "gmail.com",
"email": "decurgia@XYZ"
},
"on": "2015-01-27T16:05:41.465577"
}
}
}
如何获得那些自动设置的字段( sys.created.on
, sys.created.by
, sys.updated.on
, sys.updated.by
)?
在我对StructuredProperty
有限工作中,我发现它比直接将属性直接插入到模型中更慢,更难使用。 NDB似乎分别存储这些属性,并在检索它们时执行“联接”。 我的建议是使用“扁平”模型:
class Location(EndpointsModel):
name = ndb.StringProperty(required=True)
description = ndb.TextProperty()
address = ndb.StringProperty()
created_by = ndb.UserProperty(auto_current_user_add=True)
created_on = ndb.DateTimeProperty(auto_now_add=True)
updated_by = ndb.UserProperty(auto_current_user=True)
updated_on = ndb.DateTimeProperty(auto_now=True)
这将导致auto_
属性被自动触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.