問題:
私は以下を持っています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
このデータを POST しようとすると:
{ "name": "test", "roles": [{ "label": "test", "level": 0 }] }
エラーが発生しました ( trace ):
AttributeError: 'Role' オブジェクトに属性 '_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'>
))
property_type
エイリアスに追加すると:
@EndpointsAliasProperty(setter=roless_set, property_type=Role)
サーバーエラーが再び発生します ( trace ):
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
回避策で問題を解決しますが、問題は残ります...