0

问题:

我有以下EndpointsModels

class Role(EndpointsModel):
    label = ndb.StringProperty()
    level = ndb.IntegerProperty()

class Application(EndpointsModel):
    created = ndb.DateTimeProperty(auto_now_add=True)
    name = ndb.StringProperty()
    roles = ndb.StructuredProperty(Role, repeated=True)

和一个 API 方法:

class ApplicationApi(protorpc.remote.Service):
    @Application.method(http_method="POST",
                        request_fields=('name', 'roles'),
                        name="create",
                        path="applications")
    def ApplicationAdd(self, instance):
        return instance

当我尝试发布此数据时:

{ "name": "test", "roles": [{ "label": "test", "level": 0 }] }

我收到一个错误(跟踪):

AttributeError:“角色”对象没有属性“_Message__decoded_fields”

解决方法:

我尝试使用EndpointsAliasProperty

class ApplicationApi(protorpc.remote.Service):
    ...
    def roless_set(self, value):
        logging.info(value)
        self.roles = DEFAULT_ROLES

    @EndpointsAliasProperty(setter=roless_set)
    def roless(self):
        return getattr(self, 'roles', [])

这导致400 BadRequest

解析 ProtoRPC 请求时出错(无法解析请求内容:<type 'unicode'>字段角色的预期类型,找到 {u'level': 0, u'label': u'test'} (type <type 'dict'>))

如果我添加property_type到别名:

    @EndpointsAliasProperty(setter=roless_set, property_type=Role)

我再次收到服务器错误(跟踪):

TypeError:属性字段必须是简单 ProtoRPC 字段的子类、ProtoRPC 枚举类或 ProtoRPC 消息类。收到的角色<label=StringProperty('label'), level=IntegerProperty('level')>

有没有办法“转换”EndpointsModelProtoRPC message class有没有更好的StructuredProperty使用 POST 数据创建模型的解决方案?我找不到任何例子,如果有人知道任何链接,请分享(:

更新:

经过一些源代码的挖掘,我发现EndpointsModel.ProtoModel()可以用来将 ndb.Model 转换为 ProtoRPC 消息类

    @EndpointsAliasProperty(setter=roless_set, property_type=Role.ProtoModel())

这解决了EndpointsAliasProperty解决方法的问题,但问题仍然存在......

4

2 回答 2

0

检查这个回购:https ://github.com/zdenulo/epd-error-example 。在这里,我演示了端点原型数据存储中的错误,在最新版本中应该修复。因此,在存储库中升级到最新的 endpoints-proto-datastore,您应该有与您想要实现的目标类似的工作示例。

于 2016-02-28T20:10:21.333 回答
0

嘿,据您所知,Sasxa 有解决此问题的方法吗?我目前正在处理同样的问题,如果没有找到任何内容,我可以为此讨论启动一个新线程。

更新:创建了一个与此相关的新问题

更新:这个问题已经解决了!你可以在这里查看问题。

于 2016-03-26T20:00:46.197 回答