如何为 flask_restx 中的嵌套 json 指定架构?
How to specify schema for nested json in flask_restx?
我使用 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 行 -> fields.String: {
中。我认为这是导致问题的原因。
我尝试按如下方式使用它:
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"),
但是如果你有一个字典作为值,那么使用fields.Nested
作为参数:
"users": fields.List(fields.Nested(your_model), description="Authors"),
此外,your_model
也可以像上面那样嵌套值。
我使用 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 行 -> fields.String: {
中。我认为这是导致问题的原因。
我尝试按如下方式使用它:
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"),
但是如果你有一个字典作为值,那么使用fields.Nested
作为参数:
"users": fields.List(fields.Nested(your_model), description="Authors"),
此外,your_model
也可以像上面那样嵌套值。