4

我将 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导致了同样的问题。

4

1 回答 1

1

使用字段。嵌套

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
})
于 2022-01-09T15:27:42.810 回答