V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
skyone123
V2EX  ›  Python

fastapi 中自动生成的 openapi3 怎样设置二级目录

  •  
  •   skyone123 · 2021-12-31 18:17:36 +08:00 · 1656 次点击
    这是一个创建于 845 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问下,fastapi 中自动生成的 openapi3 怎样设置二级目录,多级目录,目前只有一级目录

    https://i.bmp.ovh/imgs/2021/12/d6d8eb88c155a402.png

    7 条回复    2022-01-18 08:17:09 +08:00
    zachlhb
        1
    zachlhb  
       2021-12-31 21:14:15 +08:00 via iPhone
    问下你用这个框架如何统一返回格式呢,好像每个接口只能返回模型
    skyone123
        2
    skyone123  
    OP
       2021-12-31 21:51:09 +08:00
    嗯,对,你先提前定义个 Response 的 model ,然后就可以统一返回了,形如
    from typing import Generic, TypeVar, Optional, Union
    from pydantic import Field
    from pydantic.generics import GenericModel

    Data = TypeVar('Data')


    class ResultResponse(GenericModel, Generic[Data]):
    """
    自定义返回模型,使用 generic-models 定义自定义模型
    https://pydantic-docs.helpmanual.io/usage/models/#generic-models
    所有返回数据都用如下格式,方便前端统一处理
    {
    code: 200,
    message: '请求成功',
    data: None
    }
    """
    code: int = Field(default=200, description='返回码')
    message: str = Field(default='请求成功', description='消息内容')
    result: Optional[Data]
    trevorwang
        3
    trevorwang  
       2022-01-11 10:53:04 +08:00
    ```

    @app.get('/users/{id}', tags=['hello'])
    def user_detail(id: int):
    filterd = list(filter(lambda i: i["id"] == id, users))
    if filterd:
    return filterd[0]
    else:
    raise HTTPException(404)

    ````
    trevorwang
        4
    trevorwang  
       2022-01-11 10:53:55 +08:00
    tags=['hello'] 这个是关键
    skyone123
        5
    skyone123  
    OP
       2022-01-12 17:32:16 +08:00
    @trevorwang 这个使用 tags=['hello']会把这个接口放到 hello 一级目录下,但是我期望的是 hello ,hello1 ...能作为二级目录,最上面能有个一级目录 h1,有个层次,但是我发现 fastapi 自带的似乎不能这样做,只能所有目录都是平级的
    skyone123
        6
    skyone123  
    OP
       2022-01-12 17:37:54 +08:00
    @trevorwang 就像这个 https://s3.bmp.ovh/imgs/2022/01/e2403f26fc83c6a2.jpg 我期望将 UploadFile 目录放在用户目录下在 fastapi 中不知如何实现
    ixuuux
        7
    ixuuux  
       2022-01-18 08:17:09 +08:00 via iPhone
    有相同的需求
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5928 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:21 · PVG 10:21 · LAX 19:21 · JFK 22:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.