endpoints-proto-datastore:如何忽略EndpointsAliasProperty setter中的必填字段?

时间:2015-11-05 16:06:25

标签: python google-app-engine endpoints-proto-datastore

我有两个课程: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()

当我尝试通过Google的API资源管理器创建新的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"
 }
}

如何创建GameSession条目而只需指定该位置的entityKey?我只是想存储密钥,这就是为什么我不想要求名字。

1 个答案:

答案 0 :(得分: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