首页 > 解决方案 > Flask-restplus:有什么方法可以大摇大摆地对命名空间进行排序?

问题描述

Namespace将条目添加到 Api 后,有什么方法可以对它们进行排序?

我正在关注文档,流程(AFAIK)似乎是:

a) 创建 API:

api = Api(version="1.0",
      title="MahApi",
      description="Mah Nice Little API",
      doc="/swagger-ui",
      strict_slashes=False)

b) 定义命名空间:

ns = Namespace("sample_namespace",
           description="sample module api",
           path='/sample_one')
...
@ns.route('/test1', endpoint='sample_ns_test1')
class TestApi(Resource):

    def get(self):
        df = mah_service.get_some_data()
        return jsonify(json.loads(df.to_json(orient='records')))

c) 向 API 添加命名空间:

api.add_namespace(mah_sample_ns)

我面临的一个问题(尽管它是一个装饰性但令人讨厌的问题)是命名空间没有以任何方式进行排序。看来我需要自己在代码中手动对命名空间进行排序。有没有更聪明的、pythonic 的方式来大摇大摆地对命名空间进行排序?

标签: pythonflaskflask-restplus

解决方案


我可能为时已晚,但我一直在想同样的事情,但使用 flask_restx 所以发布这个希望能帮助其他人。但它似乎按照您的命名空间的导入顺序进行。

例如:

from rest.endpoints.Episodes import ns as episodes_namespace
from rest.endpoints.Plex import ns as plex_namespace
from rest.endpoints.Server import ns as server_namespace

将大摇大摆地订购为:

  1. 剧集
  2. 服务器

尽管:

from rest.endpoints.Server import ns as server_namespace
from rest.endpoints.Episodes import ns as episodes_namespace
from rest.endpoints.Plex import ns as plex_namespace

将大摇大摆地订购为:

  1. 服务器
  2. 剧集

推荐阅读