如何“发布" ndb.StructuredProperty? [英] How to "POST" ndb.StructuredProperty?

查看:86
本文介绍了如何“发布" 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

当我尝试发布此数据时:

When I try to POST this data:

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

我遇到错误(跟踪):

AttributeError:角色"对象没有属性"_Message__decoded_fields"

AttributeError: 'Role' object has no attribute '_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 'dict'>))

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'>))

如果我将property_type添加到别名:

    @EndpointsAliasProperty(setter=roless_set, property_type=Role)

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

I'm getting server error again ( trace ):

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

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

Is there a way to "convert" EndpointsModel to ProtoRPC message class? Are there any better solutions for creating models with StructuredProperty using POST data? I couldn't find any examples for this, if someone knows any links, please share (:

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

After some digging through source code, I found EndpointsModel.ProtoModel() that can be used to convert ndb.Model to ProtoRPC message class

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

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

This resolves issue with EndpointsAliasProperty workaround, but the problem remains...

推荐答案

据您所知,Sasxa对此已进行了修复?我目前正在处理同一问题,如果未找到任何内容,则可以启动新的讨论线程.

Hey Sasxa has there been a fix for this as far as you know? I am currently dealing with the same issue, and I can start a new thread for this discussion if nothing has been found.

更新:创建了新问题与此链接

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

Update: This issue has been resolved! You can check out the issue here.

这篇关于如何“发布" ndb.StructuredProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆