在 OPTIONS 方法中显示棉花糖模式的描述
Displaying a description of a marshmallow Schema in OPTIONS method
我正在尝试在我的 Flask 应用程序中实现一个路由,以提供给定资源的 OPTIONS 方法和 return 与所述资源关联的棉花糖模式的描述。类似于 django 所做的事情:
{
"name": "Dataset List",
"description": "",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"id": {
"type": "integer",
"required": false,
"read_only": true,
"label": "ID"
},
"url": {
"type": "field",
"required": false,
"read_only": true,
"label": "Url"
},
"name": {
"type": "string",
"required": true,
"read_only": false,
"label": "Name",
"help_text": "The dataset name.",
"max_length": 256
}
}
}
}
我似乎无法在 Schema
或 return 文档中找到任何方法,并且 AlbumSchema.opts.fields
是空的。是否可以遍历整个字段并构建我需要的描述?符合以下内容:
desc = {f: {"required": f.required,
"type": f.__class__.__name__,
...}
for f in AlbumSchema.fields}
感谢@Pop,我设法找到了解决方案。
首先,我声明了一个通用选项方法:
class DescriptiveMethodView(views.MethodView):
name = description = uri = doc_uri = None
def options(self, id=None):
"""Base Options View.
---
description: retrieve options available for this resource.
responses:
200:
name: description
type: object
"""
d = spec.to_dict()
info = {'name': self.name,
'description': self.description % {'id': id or 'model'}}
return jsonify(info=info,
definitions=d['definitions'],
actions=d['paths'][self.doc_uri])
然后我只是继承它并覆盖那些 class 属性:
class FooView(DescriptiveMethodView):
name = 'Foos'
description = 'all Foos in the dataset'
doc_uri = uri = '/foo'
class BarView(DescriptiveMethodView):
name = 'Bar'
description = 'A bar in the dataset in the dataset'
uri = '/bar/<id>'
doc_uri = '/bar/{id}'
我正在尝试在我的 Flask 应用程序中实现一个路由,以提供给定资源的 OPTIONS 方法和 return 与所述资源关联的棉花糖模式的描述。类似于 django 所做的事情:
{
"name": "Dataset List",
"description": "",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"id": {
"type": "integer",
"required": false,
"read_only": true,
"label": "ID"
},
"url": {
"type": "field",
"required": false,
"read_only": true,
"label": "Url"
},
"name": {
"type": "string",
"required": true,
"read_only": false,
"label": "Name",
"help_text": "The dataset name.",
"max_length": 256
}
}
}
}
我似乎无法在 Schema
或 return 文档中找到任何方法,并且 AlbumSchema.opts.fields
是空的。是否可以遍历整个字段并构建我需要的描述?符合以下内容:
desc = {f: {"required": f.required,
"type": f.__class__.__name__,
...}
for f in AlbumSchema.fields}
感谢@Pop,我设法找到了解决方案。
首先,我声明了一个通用选项方法:
class DescriptiveMethodView(views.MethodView):
name = description = uri = doc_uri = None
def options(self, id=None):
"""Base Options View.
---
description: retrieve options available for this resource.
responses:
200:
name: description
type: object
"""
d = spec.to_dict()
info = {'name': self.name,
'description': self.description % {'id': id or 'model'}}
return jsonify(info=info,
definitions=d['definitions'],
actions=d['paths'][self.doc_uri])
然后我只是继承它并覆盖那些 class 属性:
class FooView(DescriptiveMethodView):
name = 'Foos'
description = 'all Foos in the dataset'
doc_uri = uri = '/foo'
class BarView(DescriptiveMethodView):
name = 'Bar'
description = 'A bar in the dataset in the dataset'
uri = '/bar/<id>'
doc_uri = '/bar/{id}'