简体   繁体   English

如何“发布” ndb.StructuredProperty?

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

Problem: 问题:

I have following EndpointsModels , 我有以下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)

and an API method: 和一个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

When I try to POST this data: 当我尝试发布此数据时:

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

I'm getting an error ( trace ): 我收到一个错误( 跟踪 ):

AttributeError: 'Role' object has no attribute '_Message__decoded_fields' AttributeError:“角色”对象没有属性“ _Message__decoded_fields”

Workaround: 解决方法:

I tried to use EndpointsAliasProperty : 我尝试使用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', [])

which results in 400 BadRequest 结果是400 BadRequest

Error parsing ProtoRPC request (Unable to parse request content: Expected type <type 'unicode'> for field roless, found {u'level': 0, u'label': u'test'} (type <type 'dict'> )) 解析ProtoRPC请求时出错(无法解析请求内容:字段角色的预期类型<type 'unicode'> type'unicode <type 'unicode'> ,找到{u'level':0,u'label':u'test'}(类型<type 'dict'> type'dict <type 'dict'> ))

If I add property_type to the alias: 如果我将property_type添加到别名:

    @EndpointsAliasProperty(setter=roless_set, property_type=Role)

I'm getting server error again ( trace ): 我再次遇到服务器错误( 跟踪 ):

TypeError: Property field must be either a subclass of a simple ProtoRPC field, a ProtoRPC enum class or a ProtoRPC message class. TypeError:属性字段必须是简单ProtoRPC字段的子类,ProtoRPC枚举类或ProtoRPC消息类。 Received Role <label=StringProperty('label'), level=IntegerProperty('level')> . 收到的角色<label=StringProperty('label'), level=IntegerProperty('level')>

Is there a way to "convert" EndpointsModel to ProtoRPC message class ? 有没有一种方法可以将 EndpointsModel转换为 ProtoRPC message class Are there any better solutions for creating models with StructuredProperty using POST data? 是否存在使用POST数据使用StructuredProperty创建模型的更好解决方案? I couldn't find any examples for this, if someone knows any links, please share (: 我找不到任何示例,如果有人知道任何链接,请分享(:

UPDATE: 更新:

After some digging through source code, I found EndpointsModel.ProtoModel() that can be used to convert ndb.Model to ProtoRPC message class 在仔细研究了源代码之后,我发现了EndpointsModel.ProtoModel()可用于将ndb.Model转换为ProtoRPC消息类。

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

This resolves issue with EndpointsAliasProperty workaround, but the problem remains... 这解决了EndpointsAliasProperty解决方法的问题,但问题仍然存在...

Check this repo: https://github.com/zdenulo/epd-error-example . 检查此仓库: https : //github.com/zdenulo/epd-error-example Here I was demonstrating error in endpoints-proto-datastore which in latest version should be fixed. 在这里,我演示了端点原始数据存储区中的错误,该错误在最新版本中应该得到修复。 So upgrade in repository to latest endpoints-proto-datastore and you should have working example which similar to what you want to achieve. 因此,在存储库中升级到最新的终结点原始数据存储,您应该有一个工作示例,该示例与您要实现的目标类似。

Hey Sasxa has there been a fix for this as far as you know? 据您所知,Sasxa对此有修复程序吗? I am currently dealing with the same issue, and I can start a new thread for this discussion if nothing has been found. 我目前正在处理同一问题,如果未发现任何问题,则可以启动新线程进行讨论。

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

Update: This issue has been resolved! 更新:此问题已解决! You can check out the issue here . 您可以在此处查看问题。

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

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