0

我有两个课程:GameSession 和 Location。每个 GameSession 都应该有一个 Location 集。

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()

当我尝试通过 Google 的 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 条目?我只是想存储密钥,这就是为什么我不想问名字。

4

1 回答 1

0

问题在于:

 _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 将通过密钥设置和获取游戏的 ID

于 2016-01-20T04:26:23.407 回答