Flask RestPlus 继承模型无法按预期工作

4c7*_*b41 5 python flask flask-restplus

所以我在 Flask RestPlus 中有这个模型:

NS = Namespace('parent')
PARENT_MODEL = NS.model('parent', {
    'parent-id': fields.String(readOnly=True,
    'parent-name': fields.String(required=True)
})
CHILD_MODEL = NS.inherit('child', SUBSCRIPTION_MODEL, {
    'child-id': fields.String(required=True, readOnly=True),
    'child-name': fields.String(required=True),
    'child-some-property': fields.String(required=True)
})

CHILD_PROPERTY_MODEL = NS.inherit('child-other-property', RESOURCE_GROUP_MODEL, {
    'child-other-property': fields.Raw(required=False)
})
Run Code Online (Sandbox Code Playgroud)

它没有按预期工作,我得到了这个输出(以及 swagger 文档上的类似结构)。

[
  {
    "parent-id": "string",
    "parent-name": "string",
    "child-id": "string",
    "child-name": "string",
    "child-some-property": "string",
    "child-other-property": {}
  }
]
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

[
  {
    "parent-id": "string",
    "parent-name": "string", {
        "child-id": "string",
        "child-name": "string",
        "child-some-property": "string",{
            "child-other-property": {}
      }
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

我可能错过了一些简单的东西,但不明白是什么。就是我在 Flask Restplus 中找出模型的咨询。

4c7*_*b41 7

NS = Namespace('sample')

child_model = NS.model('child', {
    'childid': fields.String(required=True, readOnly=True),
    'childname': fields.String(required=True),
    'data': fields.String(required=True),
    'complexdata': fields.Raw(required=False)
})

parent_model = NS.model('parent', {
    'id': fields.String(readOnly=True),
    'name': fields.String(required=True),
    'childdata': fields.List(
        fields.Nested(child_model, required=True)
        )
})
Run Code Online (Sandbox Code Playgroud)

这对我有用。看来 Flask Restplus github 已经死了,维护者没有回答。这可能会帮助某人。