首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >endpoints-proto-datastore:如何忽略EndpointsAliasProperty设置器中的必填字段?

endpoints-proto-datastore:如何忽略EndpointsAliasProperty设置器中的必填字段?
EN

Stack Overflow用户
提问于 2015-11-06 00:06:25
回答 1查看 86关注 0票数 0

我有两个类: GameSession和Location。每个GameSession都应该有一个位置集。

代码语言:javascript
复制
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时,我得到了以下错误消息:

代码语言:javascript
复制
Error parsing ProtoRPC request (Unable to parse request content: Message Location is missing required field name)

对gamesession的POST请求如下所示:

代码语言:javascript
复制
{
 "name": "aaa",
 "location": {
  "entityKey": "ahRkZXZ-Z2FtZXNlc3Npb24tY29yZXIVCxIITG9jYXRpb24YgICAgIDArwoM"
 }
}

如何在只需指定位置的entityKey的情况下创建GameSession条目?我只想存储密钥,这就是为什么我不想问名字的原因。

EN

回答 1

Stack Overflow用户

发布于 2016-01-20 12:26:23

问题出在:

代码语言:javascript
复制
 _message_fields_schema = ('entityKey', 'name', 'description', 'location')

具体地说,“entityKey”不应该出现在那里。你可以用下面这样的东西掩蔽你的钥匙

代码语言:javascript
复制
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更改为:

代码语言:javascript
复制
_message_fields_schema = ('name', 'description', 'game_session', 'address')

game_session将通过key设置并获取游戏ID的位置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33549402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档