网页 综合 更新时间 2026-11-17 03:02:30

Path Parameters and Numeric Validations - FastAPI

自动蜘蛛 fastapi.tiangolo.com 正文约 3497 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
In the same way that you can declare more validations and metadata for query parameters with Query, you can declare the same type of validations and metadata for path parameters with Path.
First, import Path from fastapi, and import Annotated:
Prefer to use the Annotated version if possible.
FastAPI added support for Annotated (and started recommending it) in version 0.95.0.
If you have an older version, you would get errors when trying to use Annotated.
Make sure you Upgrade the FastAPI version to at least 0.95.1 before using Annotated.
You can declare all the same parameters as for Query.
For example, to declare a title metadata value for the path parameter item_id you can type:
Prefer to use the Annotated version if possible.
A path parameter is always required because it has to be part of the URL path. Adding None to its type does not make it optional. A parameter declared with Path cannot have a default value.
This is probably not as important or necessary if you use Annotated.
Let's say that you want to declare the query parameter q as a required str.
And you don't need to declare anything else for that parameter, so you don't really need to use Query.
But you still need to use Path for the item_id path parameter. And you don't want to use Annotated for some reason.
Python will complain if you put a value with a "default" before a value that doesn't have a "default".
But you can re-order them, and have the value without a default (the query parameter q) first.
It doesn't matter for FastAPI. It will detect the parameters by their names, types and default declarations (Query, Path, etc), it doesn't care about the order.
So, you can declare your function as:
But keep in mind that if you use Annotated, you won't have this problem, it won't matter as you're not using the function parameter default values for Query() or Path().
Prefer to use the Annotated version if possible.
This is probably not as important or necessary if you use Annotated.
Here's a small trick that can be handy, but you won't need it often.
...Python has a little special syntax for that.
Pass *, as the first parameter of the function.
Python won't do anything with that *, but it will know that all the following parameters should be called as keyword arguments (key-value pairs), also known as kwargs. Even if they don't have a default value.
Keep in mind that if you use Annotated, as you are not using function parameter default values, you won't have this problem, and you probably won't need to use *.
Prefer to use the Annotated version if possible.
With Query and Path (and others you'll see later) you can declare number constraints.
Here, with ge=1, item_id will need to be an integer number "greater than or equal" to 1.
Prefer to use the Annotated version if possible.
The same applies for:
Prefer to use the Annotated version if possible.
Number validations also work for float values.
Here's where it becomes important to be able to declare gt and not just ge. As with it you can require, for example, that a value must be greater than 0, even if it is less than 1.
So, 0.5 would be a valid value. But 0.0 or 0 would not.
And the same for lt.
Prefer to use the Annotated version if possible.
With Query, Path (and others you haven't seen yet) you can declare metadata and string validations in the same ways as with Query Parameters and String Validations.
访问原文 返回搜索 https://fastapi.tiangolo.com/./tutorial/path-params-numeric-validations/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。