Flask RestPlus 继承模型无法按预期工作
Flask RestPlus inherit model doesn't work as expected
所以我在 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)
})
它没有按预期工作,我得到了这个输出(以及 swagger 文档上的类似结构)。
[
{
"parent-id": "string",
"parent-name": "string",
"child-id": "string",
"child-name": "string",
"child-some-property": "string",
"child-other-property": {}
}
]
而不是像这样:
[
{
"parent-id": "string",
"parent-name": "string", {
"child-id": "string",
"child-name": "string",
"child-some-property": "string",{
"child-other-property": {}
}
}
}
]
我可能遗漏了一些简单的东西,但无法理解是什么。 This 是我正在咨询以找出 Flask Restplus 中的模型。
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)
)
})
这对我有用。看起来 Flask Restplus github 已经死了,维护者没有回答。这可能会对某人有所帮助。
这是我在 serializer.py 文件中声明嵌套字段的方式
from flask_restplus import fields
from api.restplus import api
child2 = api.model('child2', {
'child2name': fields.Url(description='child2 name'),
})
child1= api.model('child1', {
'child2': fields.Nested(child2)
})
parent = {
'name': fields.String(description='name'),
'location': fields.String(description='location details'),
}
parent ["child1"] = fields.Nested(child1)
resource_resp = api.model('Response details', parent )
在view.py中的用法,我是marshaling/generatingjson和@api.marshal_with(resource_resp)
from flask import request, jsonify
from flask_restplus import Resource
from serializers import *
ns = api.namespace('apiName', description='API Description')
@ns.route('/route/<some_id>')
class ResourceClient(Resource):
@ns.response(401, "Unauthorized")
@ns.response(500, "Internal Server Error")
@api.doc(params={'some_id': 'An ID'})
@api.marshal_with(resource_resp )
def get(self, some_id):
"""
Do GET
"""
# Logic
return {"status" : "success"}
所以我在 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)
})
它没有按预期工作,我得到了这个输出(以及 swagger 文档上的类似结构)。
[
{
"parent-id": "string",
"parent-name": "string",
"child-id": "string",
"child-name": "string",
"child-some-property": "string",
"child-other-property": {}
}
]
而不是像这样:
[
{
"parent-id": "string",
"parent-name": "string", {
"child-id": "string",
"child-name": "string",
"child-some-property": "string",{
"child-other-property": {}
}
}
}
]
我可能遗漏了一些简单的东西,但无法理解是什么。 This 是我正在咨询以找出 Flask Restplus 中的模型。
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)
)
})
这对我有用。看起来 Flask Restplus github 已经死了,维护者没有回答。这可能会对某人有所帮助。
这是我在 serializer.py 文件中声明嵌套字段的方式
from flask_restplus import fields
from api.restplus import api
child2 = api.model('child2', {
'child2name': fields.Url(description='child2 name'),
})
child1= api.model('child1', {
'child2': fields.Nested(child2)
})
parent = {
'name': fields.String(description='name'),
'location': fields.String(description='location details'),
}
parent ["child1"] = fields.Nested(child1)
resource_resp = api.model('Response details', parent )
在view.py中的用法,我是marshaling/generatingjson和@api.marshal_with(resource_resp)
from flask import request, jsonify
from flask_restplus import Resource
from serializers import *
ns = api.namespace('apiName', description='API Description')
@ns.route('/route/<some_id>')
class ResourceClient(Resource):
@ns.response(401, "Unauthorized")
@ns.response(500, "Internal Server Error")
@api.doc(params={'some_id': 'An ID'})
@api.marshal_with(resource_resp )
def get(self, some_id):
"""
Do GET
"""
# Logic
return {"status" : "success"}