我有两个类: GameSession和Location。每个GameSession都应该有一个位置集。
class Location(EndpointsModel):
_message_fields_schema = ('entityKey', 'name', 'description', 'address')
name = ndb.StringProperty(required=True)
description = ndb.TextProperty()
address = ndb.StringProperty()
class GameSession(EndpointsModel):
_message_fields_schema = ('entityKey', 'name', 'description', 'location')
name = ndb.StringProperty(required=True)
description = ndb.TextProperty()
location_key = ndb.KeyProperty(kind=Location, required=True)
def location_setter(self, value):
self.location_key = ndb.Key(urlsafe=value.entityKey)
@EndpointsAliasProperty(setter=location_setter, property_type=Location.ProtoModel())
def location(self):
return self.location_key.get()当我尝试通过谷歌的API Explorer创建一个新的GameSession时,我得到了以下错误消息:
Error parsing ProtoRPC request (Unable to parse request content: Message Location is missing required field name)对gamesession的POST请求如下所示:
{
"name": "aaa",
"location": {
"entityKey": "ahRkZXZ-Z2FtZXNlc3Npb24tY29yZXIVCxIITG9jYXRpb24YgICAgIDArwoM"
}
}如何在只需指定位置的entityKey的情况下创建GameSession条目?我只想存储密钥,这就是为什么我不想问名字的原因。
发布于 2016-01-20 12:26:23
问题出在:
_message_fields_schema = ('entityKey', 'name', 'description', 'location')具体地说,“entityKey”不应该出现在那里。你可以用下面这样的东西掩蔽你的钥匙
def IdSet(self, value):
if not isinstance(value, basestring):
raise TypeError('ID must be a string.')
self.UpdateFromKey(ndb.Key(Location, value))
@EndpointsAliasProperty(setter=IdSet, required=True)
def game_session(self):
if self.key is not None:
return self.key.string_id()然后将_message_fields_schema更改为:
_message_fields_schema = ('name', 'description', 'game_session', 'address')game_session将通过key设置并获取游戏ID的位置
https://stackoverflow.com/questions/33549402
复制相似问题