你可以在 FastAPI 应用中自定义几个元数据配置。
你可以设定:
使用 title、description 和 version 来设置它们:
from fastapi import FastAPI
description = """
ChimichangApp API helps you do awesome stuff. ????
## Items
You can **read items**.
## Users
You will be able to:
* **Create users** (_not implemented_).
* **Read users** (_not implemented_).
"""
app = FastAPI(
title="ChimichangApp",
description=description,
version="0.0.1",
terms_of_service="http://example.com/terms/",
contact={
"name": "Deadpoolio the Amazing",
"url": "http://x-force.example.com/contact/",
"email": "dp@x-force.example.com",
},
license_info={
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
},
)
@app.get("/items/")
async def read_items():
return [{"name": "Katana"}]
通过这样设置,自动 API 文档看起来会像:
你也可以使用参数 openapi_tags,为用于分组路径操作的不同标签添加额外的元数据。
它接受一个列表,这个列表包含每个标签对应的一个字典。
每个字典可以包含:
让我们在带有标签的示例中为 users 和 items 试一下。
创建标签元数据并把它传递给 openapi_tags 参数:
from fastapi import FastAPI
tags_metadata = [
{
"name": "users",
"description": "Operations with users. The **login** logic is also here.",
},
{
"name": "items",
"description": "Manage items. So _fancy_ they have their own docs.",
"externalDocs": {
"description": "Items external docs",
"url": "https://fastapi.tiangolo.com/",
},
},
]
app = FastAPI(openapi_tags=tags_metadata)
@app.get("/users/", tags=["users"])
async def get_users():
return [{"name": "Harry"}, {"name": "Ron"}]
@app.get("/items/", tags=["items"])
async def get_items():
return [{"name": "wand"}, {"name": "flying broom"}]
注意你可以在描述内使用 Markdown,例如「login」会显示为粗体(login)以及「fancy」会显示为斜体(fancy)。
提示
不必为你使用的所有标签都添加元数据。
将 tags 参数和路径操作(以及 APIRouter)一起使用,将其分配给不同的标签:
from fastapi import FastAPI
tags_metadata = [
{
"name": "users",
"description": "Operations with users. The **login** logic is also here.",
},
{
"name": "items",
"description": "Manage items. So _fancy_ they have their own docs.",
"externalDocs": {
"description": "Items external docs",
"url": "https://fastapi.tiangolo.com/",
},
},
]
app = FastAPI(openapi_tags=tags_metadata)
@app.get("/users/", tags=["users"])
async def get_users():
return [{"name": "Harry"}, {"name": "Ron"}]
@app.get("/items/", tags=["items"])
async def get_items():
return [{"name": "wand"}, {"name": "flying broom"}]
信息
阅读更多关于标签的信息路径操作配置。
如果你现在查看文档,它们会显示所有附加的元数据:
每个标签元数据字典的顺序也定义了在文档用户界面显示的顺序。
例如按照字母顺序,即使 users 排在 items 之后,它也会显示在前面,因为我们将它的元数据添加为列表内的第一个字典。
默认情况下,OpenAPI 模式服务于 /openapi.json。
但是你可以通过参数 openapi_url 对其进行配置。
例如,将其设置为服务于 /api/v1/openapi.json:
from fastapi import FastAPI
app = FastAPI(openapi_url="/api/v1/openapi.json")
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
如果你想完全禁用 OpenAPI 模式,可以将其设置为 openapi_url=None,这样也会禁用使用它的文档用户界面。
你可以配置两个文档用户界面,包括:
例如,设置 Swagger UI 服务于 /documentation 并禁用 ReDoc:
from fastapi import FastAPI
app = FastAPI(docs_url="/documentation", redoc_url=None)
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
FastAPI默认会使用JSONResponse返回响应。你可以通过直接返回Response来重载它,参见直接返回响应。但如果你直接返回Response,...
如果在旧迁移中引用了自定义模型字段,则从项目或第三方应用中删除这些字段将导致问题。为了解决这种情况,Django 提供了一些模...
语法Django 模板是使用 Django 模板语言标记的一个文本文档或Python字符串。模板引擎可以识别和解释一些构造。主要是变量和标签...
当激活 SessionMiddleware 后,每个 HttpRequest 对象(任何 Django 视图函数的第一个参数) 将得到一个 session 属...
安装Python作为一个 Python 网络框架,Django 需要 Python。Python 包含了一个名为 SQLite 的轻量级数据库,所以你暂时不必自行...
在后台,Django将如何以及在哪里存储文件的决策委托给文件存储系统。这个对象实际上理解文件系统、打开和读取文件等。Django 的...
下表列出了JTextComponent类中包含的某些常用的文本组件方法。...我们可以根据两个标准对文本组件进行分类:number of lines in t...
Java Swing教程 -Java Swing外观和感觉Swing支持可插拔的外观(L F)。Swing支持可插拔的外观(L F)。...lafClassName参数是提供L F...