问题:
我有以下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'>
字段角色的预期类型,找到 {u'level': 0, u'label': u'test'} (type<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
?StructuredProperty
使用 POST 数据创建模型的解决方案?我找不到任何例子,如果有人知道任何链接,请分享(:
更新:
经过一些源代码的挖掘,我发现EndpointsModel.ProtoModel()
可以用来将 ndb.Model 转换为 ProtoRPC 消息类
@EndpointsAliasProperty(setter=roless_set, property_type=Role.ProtoModel())
这解决了EndpointsAliasProperty
解决方法的问题,但问题仍然存在......