繁体   English   中英

如何“发布” ndb.StructuredProperty?

[英]How to “POST” ndb.StructuredProperty?

问题:

我有以下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'> type'unicode <type 'unicode'> ,找到{u'level':0,u'label':u'test'}(类型<type 'dict'> type'dict <type 'dict'> ))

如果我将property_type添加到别名:

    @EndpointsAliasProperty(setter=roless_set, property_type=Role)

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

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

有没有一种方法可以将 EndpointsModel转换为 ProtoRPC message class 是否存在使用POST数据使用StructuredProperty创建模型的更好解决方案? 我找不到任何示例,如果有人知道任何链接,请分享(:

更新:

在仔细研究了源代码之后,我发现了EndpointsModel.ProtoModel()可用于将ndb.Model转换为ProtoRPC消息类。

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

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

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

据您所知,Sasxa对此有修复程序吗? 我目前正在处理同一问题,如果未发现任何问题,则可以启动新线程进行讨论。

created a new issue linked to this one 创建了与此相关的新问题

更新:此问题已解决! 您可以在此处查看问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM