[英]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.