0

問題:

私は以下を持っています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か?StructuredPropertyPOST データを使用してモデルを作成するためのより良いソリューションはありますか? これに関する例は見つかりませんでした。誰かがリンクを知っている場合は、共有してください (:

アップデート:

ソース コードを掘り下げた後、EndpointsModel.ProtoModel()ndb.Model を ProtoRPC メッセージ クラスに変換するために使用できることがわかりました。

    @EndpointsAliasProperty(setter=roless_set, property_type=Role.ProtoModel())

これはEndpointsAliasProperty回避策で問題を解決しますが、問題は残ります...

4

2 に答える 2

0

このリポジトリを確認してください: https://github.com/zdenulo/epd-error-example。ここでは、endpoints-proto-datastore のエラーを示していましたが、これは最新バージョンで修正する必要があります。したがって、リポジトリを最新の endpoints-proto-datastore にアップグレードすると、達成したいものと同様の作業例が得られるはずです。

于 2016-02-28T20:10:21.333 に答える
0

Sasxa さん、あなたの知る限り、これに対する修正はありますか? 現在、同じ問題に取り組んでおり、何も見つからない場合は、このディスカッションのために新しいスレッドを開始できます。

更新:これにリンクされた新しい問題を作成しました

更新:この問題は解決されました! ここで問題を確認できます。

于 2016-03-26T20:00:46.197 に答える