FastAPI 教程翻译 - 用户指南 4 - 查询参数

FastAPI 教程翻译 - 用户指南 4 - 查询参数

FastAPI Tutorial - User Guide - Query Parameters

When you declare other function parameters that are not part of the path parameters, they are automatically interpreted as “query” parameters.

声明不属于路径参数的其他视图函数参数时,它们将自动解释为『查询』参数。

from fastapi import FastAPI

app = FastAPI()

fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]


@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
    return fake_items_db[skip : skip + limit]

The query is the set of key-value pairs that go after the ? in a URL, separated by & characters.

查询是一系列的键值对,这些键值对位于 URL 中的 ? 字符之后,并以 & 字符分隔。

For example, in the url:

例如,在如下 URL 中:

http://127.0.0.1:8000/items/?skip=0&limit=10

…the query parameters are:

…… 查询参数如下:

  • skip:with a value of 0

    skip:值为 0

  • limit:with a value of 10

    limit:值为 10

As they are part of the URL, they are “naturally” strings.

由于它们是 URL 的一部分,因此它们『自然而然』是字符串。

But when you declare them with Python types (in the example above, as int), they are converted to that type and validated against it.

但是,当您使用 Python 类型声明它们时(在上面的示例中为 int),它们将转换为该类型并针对该类型进行验证。

All the same process that applied for path parameters also applies for query parameters:

应用于路径参数的所有过程也适用于查询参数:

  • Editor support (obviously)

    编辑器支持(显然)

  • Data “parsing”

    数据『解析』

  • Data validation

    数据验证

  • Automatic documentation

    自动文档

Defaults

默认值

As query parameters are not a fixed part of a path, they can be optional and can have default values.

由于查询参数不是路径的固定部分,因此它们可以是可选的,并且可以具有默认值。

In the example above they have default values of skip=0 and limit=10.

在上面的示例中,它们的默认值为 skip=0limit=10

So, going to the URL:

因此,转到这个 URL:

http://127.0.0.1:8000/items/

would be the same as going to:

将与执行以下操作相同:

http://127.0.0.1:8000/items/?skip=0&limit=10

But if you go to, for example:

但是,如果您要去这个 URL:

http://127.0.0.1:8000/items/?skip=20

The parameter values in your function will be:

视图函数中的参数值为:

  • skip=20:because you set it in the URL

    skip=20:因为您在 URL 中进行了设置

  • limit=10:because that was the default value

    limit=10:因为那是默认值

Optional parameters

可选查询参数

The same way, you can declare optional query parameters, by setting their default to None:

同样,您可以通过将可选查询参数的默认值设置为 None 来声明可选查询参数:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None):
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}

In this case, the function parameter q will be optional, and will be None by default.

在这种情况下,视图函数参数 q 将是可选的,默认情况下将为 None

Check

检查

Also notice that FastAPI is smart enough to notice that the path parameter item_id is a path parameter and q is not, so, it’s a query parameter.

还请注意,FastAPI 非常聪明,足以注意到参数 item_id 是路径参数,而 q 不是路径参数,因此它是一个查询参数。

Query parameter type conversion

查询参数类型转换

You can also declare bool types, and they will be converted:

您还可以将查询参数声明为 bool 类型,传入的参数将会被转换:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str = None, short: bool = False):
    item = {"item_id": item_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item

In this case, if you go to:

在这种情况下,如果您要执行以下操作:

http://127.0.0.1:8000/items/foo?short=1

or

或者

http://127.0.0.1:8000/items/foo?short=True

or

或者

http://127.0.0.1:8000/items/foo?short=true

or

或者

http://127.0.0.1:8000/items/foo?short=on

or

或者

http://127.0.0.1:8000/items/foo?short=yes

or any other case variation (uppercase, first letter in uppercase, etc), your function will see the parameter short with a bool value of True. Otherwise as False.

或任何其他大小写变体(全部大写,首字母大写等),您的函数将会把参数 short 转换为 bool 类型,值为 True,否则为 False

Multiple path and query parameters

多个路径和查询参数

You can declare multiple path parameters and query parameters at the same time, FastAPI knows which is which.

您可以同时声明多个路径参数和查询参数,FastAPI 知道区分它们。

And you don’t have to declare them in any specific order.

而且您不必以任何特定顺序声明它们。

They will be detected by name:

将通过名称检测到它们:

from fastapi import FastAPI

app = FastAPI()


@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
    user_id: int, item_id: str, q: str = None, short: bool = False
):
    item = {"item_id": item_id, "owner_id": user_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item

Required query parameters

必需的查询参数

When you declare a default value for non-path parameters (for now, we have only seen query parameters), then it is not required.

当您声明非路径参数的默认值时(目前,我们仅看到查询参数),则可以不需要此参数(可选的)。

If you don’t want to add a specific value but just make it optional, set the default as None.

如果您不想添加特定值,而只是使其成为可选参数,则将默认值设置为 None

但是,当您必需一个查询参数时,就不能声明任何默认值:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
    item = {"item_id": item_id, "needy": needy}
    return item

Here the query parameter needy is a required query parameter of type str.

这里的查询参数 needy 是类型为 str 的必需查询参数。

If you open in your browser a URL like:

如果您在浏览器中打开一个 URL,例如:

http://127.0.0.1:8000/items/foo-item

…without adding the required parameter needy, you will see an error like:

…… 未添加必需的参数 needy,您将看到如下的错误:

{
    "detail": [
        {
            "loc": [
                "query",
                "needy"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}

As needy is a required parameter, you would need to set it in the URL:

由于 needy 是必需参数,因此您需要在 URL 中进行设置:

http://127.0.0.1:8000/items/foo-item?needy=sooooneedy

…this would work:

…… 这将实现:

{
    "item_id": "foo-item",
    "needy": "sooooneedy"
}

And of course, you can define some parameters as required, some as having a default value, and some entirely optional:

当然,您可以根据需要定义一些参数,一些具有默认值,而某些则完全可选:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int = None):
    item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
    return item

In this case, there are 3 query parameters:

在这种情况下,有 3 个查询参数:

  • needy:a required str

    needy:必需的 str

  • skip:an int with a default value of 0

    skip:默认值为 0int

  • limit:an optional int

    limit:可选的 int

Tip

提示

You could also use Enums the same way as with Path Parameters.

您也可以使用 Enum 类,就像 路径参数 一样。

Optional type declarations

可选类型声明

Warning

警告

This might be an advanced use case.

这可能是高级用法。

You might want to skip it.

您可能要跳过它。

If you are using mypy it could complain with type declarations like:

如果您使用的是 mypy,它可能会对如下的类型声明:

limit: int = None

With an error like:

会出现这样的错误:

Incompatible types in assignment (expression has type "None", variable has type "int")

In those cases you can use Optional to tell mypy that the value could be None, like:

在这种情况下,您可以使用 Optional 来告诉 mypy 该参数的值可以为 None,例如:

from typing import Optional

limit: Optional[int] = None

In a path operation that could look like:

路径操作中,如下方式使用:

from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(item_id: str, limit: Optional[int] = None):
    item = {"item_id": item_id, "limit": limit}
    return item
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fastapi-mysql-generator 是一个用于快速生成FastAPI和MySQL的框架的工具。FastAPI是一个现代、快速(高性能)的web框架,而MySQL是一个流行的关系型数据库。 使用 fastapi-mysql-generator,可以从一个简单的命令行界面中生成 FastAPI 应用程序,并与 MySQL 数据库集成。这个工具可以自动创建数据库表和模型(Model),并提供一组 API 端点,用于执行常见的 CRUD(创建、读取、更新和删除)操作。 fastapi-mysql-generator 的主要优势之一是它的简单易用性。无论是初学者还是有经验的开发人员,都可以快速上手并生成一个完整的基于FastAPI和MySQL的应用程序。只需要几个简单的步骤,就可以生成项目的基本结构和代码。同时,fastapi-mysql-generator 还提供了一些配置选项,可以根据需求进行自定义设置,以满足特定的应用程序需求。 这个工具还提供了一些有用的特性,以增强开发的效率和便利性。例如,它可以自动生成 API 文档,包括请求和响应模型的文档说明。此外,fastapi-mysql-generator 还支持身份验证和授权功能,以确保 API 路由的安全性。 总而言之,fastapi-mysql-generator 是一个快速生成 FastAPI 和 MySQL 应用程序的方便工具。它简化了应用程序的开发过程,并提供了一些有用的特性,以提高开发效率和便利性。无论是初学者还是有经验的开发人员,都可以受益于这个工具的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值