[英]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')>
。
有没有一种方法可以将
是否存在使用POST数据使用EndpointsModel
转换为
ProtoRPC message class
?
StructuredProperty
创建模型的更好解决方案? 我找不到任何示例,如果有人知道任何链接,请分享(:
在仔细研究了源代码之后,我发现了EndpointsModel.ProtoModel()
可用于将ndb.Model转换为ProtoRPC消息类。
@EndpointsAliasProperty(setter=roless_set, property_type=Role.ProtoModel())
这解决了EndpointsAliasProperty
解决方法的问题,但问题仍然存在...
检查此仓库: https : //github.com/zdenulo/epd-error-example 。 在这里,我演示了端点原始数据存储区中的错误,该错误在最新版本中应该得到修复。 因此,在存储库中升级到最新的终结点原始数据存储,您应该有一个工作示例,该示例与您要实现的目标类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.