问题描述
我关注了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 }] }
我遇到错误(跟踪):
解决方法:
我尝试使用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
如果我将property_type
添加到别名:
@EndpointsAliasProperty(setter=roless_set, property_type=Role)
我再次遇到服务器错误(跟踪):
I'm getting server error again ( trace ):
是否有更好的解决方案,可以使用POST数据使用StructuredProperty
创建模型?我找不到任何示例,如果有人知道任何链接,请分享(:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!