[英]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.