如何在flask_restx中为嵌套json指定模式



我使用flask_restx进行型锻API。版本如下:

python - 3.8.0
flask  - 2.0.2
flask_restx - 0.5.1

以下是我需要为其指定模式的嵌套json:

dr_status_fields = app_api.model('DR Status',{
fields.String: {
"elasticsearch": {
"backup_status": fields.String,
"backup_folder": fields.String,
},
"mongodb": {
"backup_status": fields.String,
"backup_folder": fields.String,
},
"postgresdb": {
"backup_status": fields.String,
"backup_folder": fields.String,
},
"overall_backup_status": fields.String
}
})

但当我在浏览器中加载swagger URL时,我在命令行中出现错误:

Unable to render schema
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/flask_restx/api.py", line 571, in __schema__
self._schema = Swagger(self).as_dict()
File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 268, in as_dict
"definitions": self.serialize_definitions() or None,
File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 623, in serialize_definitions
return dict(
File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 624, in <genexpr>
(name, model.__schema__)
File "/usr/local/lib/python3.8/site-packages/flask_restx/model.py", line 76, in __schema__
schema = self._schema
File "/usr/local/lib/python3.8/site-packages/flask_restx/model.py", line 159, in _schema
properties[name] = field.__schema__
AttributeError: 'dict' object has no attribute '__schema__'

我试着查看了flask_restx的文档中的任何示例用法。但找不到。请就同一提供帮助

更新

根据r-m-n的答案,我已经尝试应用相同的答案。但这里的问题是存在一个关键,即在上面定义的方案的第2行中->CCD_ 2。我认为这就是问题的根源。我试着如下使用它:

dr_status_fields = app_api.model('Backup and restore related fields', {
"backup_status": fields.String,
"backup_folder": fields.String,
})
dr_db_fields = app_api.model('DR Status', {
"elasticsearch": fields.Nested(dr_status_fields),
"mongodb": fields.Nested(dr_status_fields),
"postgresdb": fields.Nested(dr_status_fields),
"overall_backup_status": fields.String
})
dr_timestamp_field = app_api.model('Timestamp', {
fields.String: fields.Nested(dr_db_fields)
})

在这里,dr_timestamp_field导致了相同的问题。

使用字段。嵌套:

backup_fields = api.model('Backup fields', {
"backup_status": fields.String,
"backup_folder": fields.String
})
dr_status_fields = api.model('DR Status', {
"elasticsearch": fields.Nested(backup_fields),
"mongodb": fields.Nested(backup_fields),
"postgresdb": fields.Nested(backup_fields),
"overall_backup_status": fields.String
})

在某些情况下,您的值是字符串列表:

"titles": fields.List(fields.String, description="Study books"),

但如果您有dict作为值,则使用fields.Nested作为参数:

"users": fields.List(fields.Nested(your_model), description="Authors"),

此外,your_model还可以如上所述嵌套值。

相关内容

  • 没有找到相关文章

最新更新