mirror of https://github.com/tiangolo/fastapi.git
📝 Update includes in `docs/en/docs/tutorial/path-params.md` (#12811)
This commit is contained in:
parent
23cb1f8334
commit
6f671b8b5a
|
|
@ -2,9 +2,7 @@
|
|||
|
||||
You can declare path "parameters" or "variables" with the same syntax used by Python format strings:
|
||||
|
||||
```Python hl_lines="6-7"
|
||||
{!../../docs_src/path_params/tutorial001.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial001.py hl[6:7] *}
|
||||
|
||||
The value of the path parameter `item_id` will be passed to your function as the argument `item_id`.
|
||||
|
||||
|
|
@ -18,9 +16,7 @@ So, if you run this example and go to <a href="http://127.0.0.1:8000/items/foo"
|
|||
|
||||
You can declare the type of a path parameter in the function, using standard Python type annotations:
|
||||
|
||||
```Python hl_lines="7"
|
||||
{!../../docs_src/path_params/tutorial002.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial002.py hl[7] *}
|
||||
|
||||
In this case, `item_id` is declared to be an `int`.
|
||||
|
||||
|
|
@ -123,17 +119,13 @@ And then you can also have a path `/users/{user_id}` to get data about a specifi
|
|||
|
||||
Because *path operations* are evaluated in order, you need to make sure that the path for `/users/me` is declared before the one for `/users/{user_id}`:
|
||||
|
||||
```Python hl_lines="6 11"
|
||||
{!../../docs_src/path_params/tutorial003.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial003.py hl[6,11] *}
|
||||
|
||||
Otherwise, the path for `/users/{user_id}` would match also for `/users/me`, "thinking" that it's receiving a parameter `user_id` with a value of `"me"`.
|
||||
|
||||
Similarly, you cannot redefine a path operation:
|
||||
|
||||
```Python hl_lines="6 11"
|
||||
{!../../docs_src/path_params/tutorial003b.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial003b.py hl[6,11] *}
|
||||
|
||||
The first one will always be used since the path matches first.
|
||||
|
||||
|
|
@ -149,9 +141,7 @@ By inheriting from `str` the API docs will be able to know that the values must
|
|||
|
||||
Then create class attributes with fixed values, which will be the available valid values:
|
||||
|
||||
```Python hl_lines="1 6-9"
|
||||
{!../../docs_src/path_params/tutorial005.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial005.py hl[1,6:9] *}
|
||||
|
||||
/// info
|
||||
|
||||
|
|
@ -169,9 +159,7 @@ If you are wondering, "AlexNet", "ResNet", and "LeNet" are just names of Machine
|
|||
|
||||
Then create a *path parameter* with a type annotation using the enum class you created (`ModelName`):
|
||||
|
||||
```Python hl_lines="16"
|
||||
{!../../docs_src/path_params/tutorial005.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial005.py hl[16] *}
|
||||
|
||||
### Check the docs
|
||||
|
||||
|
|
@ -187,17 +175,13 @@ The value of the *path parameter* will be an *enumeration member*.
|
|||
|
||||
You can compare it with the *enumeration member* in your created enum `ModelName`:
|
||||
|
||||
```Python hl_lines="17"
|
||||
{!../../docs_src/path_params/tutorial005.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial005.py hl[17] *}
|
||||
|
||||
#### Get the *enumeration value*
|
||||
|
||||
You can get the actual value (a `str` in this case) using `model_name.value`, or in general, `your_enum_member.value`:
|
||||
|
||||
```Python hl_lines="20"
|
||||
{!../../docs_src/path_params/tutorial005.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial005.py hl[20] *}
|
||||
|
||||
/// tip
|
||||
|
||||
|
|
@ -211,9 +195,7 @@ You can return *enum members* from your *path operation*, even nested in a JSON
|
|||
|
||||
They will be converted to their corresponding values (strings in this case) before returning them to the client:
|
||||
|
||||
```Python hl_lines="18 21 23"
|
||||
{!../../docs_src/path_params/tutorial005.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial005.py hl[18,21,23] *}
|
||||
|
||||
In your client you will get a JSON response like:
|
||||
|
||||
|
|
@ -252,9 +234,7 @@ In this case, the name of the parameter is `file_path`, and the last part, `:pat
|
|||
|
||||
So, you can use it with:
|
||||
|
||||
```Python hl_lines="6"
|
||||
{!../../docs_src/path_params/tutorial004.py!}
|
||||
```
|
||||
{* ../../docs_src/path_params/tutorial004.py hl[6] *}
|
||||
|
||||
/// tip
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue