To achieve that, import `JSONResponse`, and return your content there directly, setting the `status_code` that you want:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4 26"
- {!> ../../../docs_src/additional_status_codes/tutorial001_an.py!}
+ ```Python hl_lines="4 25"
+ {!> ../../../docs_src/additional_status_codes/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="4 25"
{!> ../../../docs_src/additional_status_codes/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 25"
- {!> ../../../docs_src/additional_status_codes/tutorial001_an_py310.py!}
+ ```Python hl_lines="4 26"
+ {!> ../../../docs_src/additional_status_codes/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="4 25"
- {!> ../../../docs_src/additional_status_codes/tutorial001.py!}
+ ```Python hl_lines="2 23"
+ {!> ../../../docs_src/additional_status_codes/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="2 23"
- {!> ../../../docs_src/additional_status_codes/tutorial001_py310.py!}
+ ```Python hl_lines="4 25"
+ {!> ../../../docs_src/additional_status_codes/tutorial001.py!}
```
!!! warning
To do that, we declare a method `__call__`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/dependencies/tutorial011_an.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/dependencies/tutorial011_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
And now, we can use `__init__` to declare the parameters of the instance that we can use to "parameterize" the dependency:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/dependencies/tutorial011_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/dependencies/tutorial011_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
We could create an instance of this class with:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial011_an.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial011_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
...and pass whatever that returns as the value of the dependency in our *path operation function* as the parameter `fixed_content_included`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="21"
- {!> ../../../docs_src/dependencies/tutorial011_an.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="22"
- {!> ../../../docs_src/dependencies/tutorial011_an_py39.py!}
+ ```Python hl_lines="21"
+ {!> ../../../docs_src/dependencies/tutorial011_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Let's start with a simple FastAPI application:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9-11 14-15 18 19 23"
- {!> ../../../docs_src/generate_clients/tutorial001.py!}
+ ```Python hl_lines="7-9 12-13 16-17 21"
+ {!> ../../../docs_src/generate_clients/tutorial001_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7-9 12-13 16-17 21"
- {!> ../../../docs_src/generate_clients/tutorial001_py39.py!}
+ ```Python hl_lines="9-11 14-15 18 19 23"
+ {!> ../../../docs_src/generate_clients/tutorial001.py!}
```
Notice that the *path operations* define the models they use for request payload and response payload, using the models `Item` and `ResponseMessage`.
For example, you could have a section for **items** and another section for **users**, and they could be separated by tags:
+=== "Python 3.9+"
-=== "Python 3.6 and above"
-
- ```Python hl_lines="23 28 36"
- {!> ../../../docs_src/generate_clients/tutorial002.py!}
+ ```Python hl_lines="21 26 34"
+ {!> ../../../docs_src/generate_clients/tutorial002_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="21 26 34"
- {!> ../../../docs_src/generate_clients/tutorial002_py39.py!}
+ ```Python hl_lines="23 28 36"
+ {!> ../../../docs_src/generate_clients/tutorial002.py!}
```
### Generate a TypeScript Client with Tags
You can then pass that custom function to **FastAPI** as the `generate_unique_id_function` parameter:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8-9 12"
- {!> ../../../docs_src/generate_clients/tutorial003.py!}
+ ```Python hl_lines="6-7 10"
+ {!> ../../../docs_src/generate_clients/tutorial003_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6-7 10"
- {!> ../../../docs_src/generate_clients/tutorial003_py39.py!}
+ ```Python hl_lines="8-9 12"
+ {!> ../../../docs_src/generate_clients/tutorial003.py!}
```
### Generate a TypeScript Client with Custom Operation IDs
* It returns an object of type `HTTPBasicCredentials`:
* It contains the `username` and `password` sent.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="2 7 11"
- {!> ../../../docs_src/security/tutorial006_an.py!}
+ ```Python hl_lines="4 8 12"
+ {!> ../../../docs_src/security/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 8 12"
- {!> ../../../docs_src/security/tutorial006_an_py39.py!}
+ ```Python hl_lines="2 7 11"
+ {!> ../../../docs_src/security/tutorial006_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Then we can use `secrets.compare_digest()` to ensure that `credentials.username` is `"stanleyjobson"`, and that `credentials.password` is `"swordfish"`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="1 12-24"
- {!> ../../../docs_src/security/tutorial007_an.py!}
+ {!> ../../../docs_src/security/tutorial007_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="1 12-24"
- {!> ../../../docs_src/security/tutorial007_an_py39.py!}
+ {!> ../../../docs_src/security/tutorial007_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
After detecting that the credentials are incorrect, return an `HTTPException` with a status code 401 (the same returned when no credentials are provided) and add the header `WWW-Authenticate` to make the browser show the login prompt again:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="26-30"
- {!> ../../../docs_src/security/tutorial007_an.py!}
+ {!> ../../../docs_src/security/tutorial007_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="26-30"
- {!> ../../../docs_src/security/tutorial007_an_py39.py!}
+ {!> ../../../docs_src/security/tutorial007_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
First, let's quickly see the parts that change from the examples in the main **Tutorial - User Guide** for [OAuth2 with Password (and hashing), Bearer with JWT tokens](../../tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. Now using OAuth2 scopes:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="2 4 8 12 47 65 106 108-116 122-125 129-135 140 156"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="4 8 12 46 64 105 107-115 121-124 128-134 139 155"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="2 4 8 12 46 64 105 107-115 121-124 128-134 139 155"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 8 12 46 64 105 107-115 121-124 128-134 139 155"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="2 4 8 12 47 65 106 108-116 122-125 129-135 140 156"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="2 4 8 12 46 64 105 107-115 121-124 128-134 139 153"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="3 7 11 45 63 104 106-114 120-123 127-133 138 152"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3 7 11 45 63 104 106-114 120-123 127-133 138 152"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="2 4 8 12 46 64 105 107-115 121-124 128-134 139 153"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
Now let's review those changes step by step.
The `scopes` parameter receives a `dict` with each scope as a key and the description as the value:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="63-66"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="62-65"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="62-65"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="62-65"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="63-66"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="62-65"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="61-64"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="61-64"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="62-65"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
Because we are now declaring those scopes, they will show up in the API docs when you log-in/authorize.
But in your application, for security, you should make sure you only add the scopes that the user is actually able to have, or the ones you have predefined.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="156"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="155"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="155"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="155"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="156"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="153"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="152"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="152"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="153"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
## Declare scopes in *path operations* and dependencies
We are doing it here to demonstrate how **FastAPI** handles scopes declared at different levels.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4 140 171"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="4 139 170"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="4 139 170"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 139 170"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="4 140 171"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="4 139 166"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="3 138 165"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3 138 165"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="4 139 166"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
!!! info "Technical Details"
This `SecurityScopes` class is similar to `Request` (`Request` was used to get the request object directly).
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="8 106"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="8 105"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="8 105"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8 105"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="8 106"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8 105"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="7 104"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7 104"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="8 105"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
## Use the `scopes`
In this exception, we include the scopes required (if any) as a string separated by spaces (using `scope_str`). We put that string containing the scopes in the `WWW-Authenticate` header (this is part of the spec).
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="106 108-116"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="105 107-115"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="105 107-115"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="105 107-115"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="106 108-116"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="105 107-115"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="104 106-114"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="104 106-114"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="105 107-115"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
## Verify the `username` and data shape
We also verify that we have a user with that username, and if not, we raise that same exception we created before.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="47 117-128"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="46 116-127"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="46 116-127"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="46 116-127"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="47 117-128"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="46 116-127"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="45 115-126"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="45 115-126"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="46 116-127"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
## Verify the `scopes`
For this, we use `security_scopes.scopes`, that contains a `list` with all these scopes as `str`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="129-135"
- {!> ../../../docs_src/security/tutorial005_an.py!}
+ ```Python hl_lines="128-134"
+ {!> ../../../docs_src/security/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="128-134"
{!> ../../../docs_src/security/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="128-134"
- {!> ../../../docs_src/security/tutorial005_an_py310.py!}
+ ```Python hl_lines="129-135"
+ {!> ../../../docs_src/security/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="128-134"
- {!> ../../../docs_src/security/tutorial005.py!}
+ ```Python hl_lines="127-133"
+ {!> ../../../docs_src/security/tutorial005_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/security/tutorial005_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="127-133"
- {!> ../../../docs_src/security/tutorial005_py310.py!}
+ ```Python hl_lines="128-134"
+ {!> ../../../docs_src/security/tutorial005.py!}
```
## Dependency tree and scopes
Now we create a dependency that returns a new `config.Settings()`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="6 12-13"
- {!> ../../../docs_src/settings/app02_an/main.py!}
+ {!> ../../../docs_src/settings/app02_an_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="6 12-13"
- {!> ../../../docs_src/settings/app02_an_py39/main.py!}
+ {!> ../../../docs_src/settings/app02_an/main.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
And then we can require it from the *path operation function* as a dependency and use it anywhere we need it.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="17 19-21"
- {!> ../../../docs_src/settings/app02_an/main.py!}
+ {!> ../../../docs_src/settings/app02_an_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="17 19-21"
- {!> ../../../docs_src/settings/app02_an_py39/main.py!}
+ {!> ../../../docs_src/settings/app02_an/main.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
But as we are using the `@lru_cache()` decorator on top, the `Settings` object will be created only once, the first time it's called. ✔️
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="1 11"
- {!> ../../../docs_src/settings/app03_an/main.py!}
+ {!> ../../../docs_src/settings/app03_an_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="1 11"
- {!> ../../../docs_src/settings/app03_an_py39/main.py!}
+ {!> ../../../docs_src/settings/app03_an/main.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
And then **FastAPI** will call that override instead of the original dependency.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="29-30 33"
- {!> ../../../docs_src/dependency_testing/tutorial001_an.py!}
+ ```Python hl_lines="26-27 30"
+ {!> ../../../docs_src/dependency_testing/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="28-29 32"
{!> ../../../docs_src/dependency_testing/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="26-27 30"
- {!> ../../../docs_src/dependency_testing/tutorial001_an_py310.py!}
+ ```Python hl_lines="29-30 33"
+ {!> ../../../docs_src/dependency_testing/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="28-29 32"
- {!> ../../../docs_src/dependency_testing/tutorial001.py!}
+ ```Python hl_lines="24-25 28"
+ {!> ../../../docs_src/dependency_testing/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="24-25 28"
- {!> ../../../docs_src/dependency_testing/tutorial001_py310.py!}
+ ```Python hl_lines="28-29 32"
+ {!> ../../../docs_src/dependency_testing/tutorial001.py!}
```
!!! tip
They work the same way as for other FastAPI endpoints/*path operations*:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="69-70 83"
- {!> ../../../docs_src/websockets/tutorial002_an.py!}
+ ```Python hl_lines="68-69 82"
+ {!> ../../../docs_src/websockets/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="68-69 82"
{!> ../../../docs_src/websockets/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="68-69 82"
- {!> ../../../docs_src/websockets/tutorial002_an_py310.py!}
+ ```Python hl_lines="69-70 83"
+ {!> ../../../docs_src/websockets/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="68-69 81"
- {!> ../../../docs_src/websockets/tutorial002.py!}
+ ```Python hl_lines="66-67 79"
+ {!> ../../../docs_src/websockets/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="66-67 79"
- {!> ../../../docs_src/websockets/tutorial002_py310.py!}
+ ```Python hl_lines="68-69 81"
+ {!> ../../../docs_src/websockets/tutorial002.py!}
```
!!! info
When a WebSocket connection is closed, the `await websocket.receive_text()` will raise a `WebSocketDisconnect` exception, which you can then catch and handle like in this example.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="81-83"
- {!> ../../../docs_src/websockets/tutorial003.py!}
+ ```Python hl_lines="79-81"
+ {!> ../../../docs_src/websockets/tutorial003_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="79-81"
- {!> ../../../docs_src/websockets/tutorial003_py39.py!}
+ ```Python hl_lines="81-83"
+ {!> ../../../docs_src/websockets/tutorial003.py!}
```
To try it out:
As Python advances, **newer versions** come with improved support for these type annotations and in many cases you won't even need to import and use the `typing` module to declare the type annotations.
-If you can choose a more recent version of Python for your project, you will be able to take advantage of that extra simplicity. See some examples below.
+If you can choose a more recent version of Python for your project, you will be able to take advantage of that extra simplicity.
+
+In all the docs there are examples compatible with each version of Python (when there's a difference).
+
+For example "**Python 3.6+**" means it's compatible with Python 3.6 or above (including 3.7, 3.8, 3.9, 3.10, etc). And "**Python 3.9+**" means it's compatible with Python 3.9 or above (including 3.10, etc).
+
+If you can use the **latest versions of Python**, use the examples for the latest version, those will have the **best and simplest syntax**, for example, "**Python 3.10+**".
#### List
For example, let's define a variable to be a `list` of `str`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
+
+ Declare the variable, with the same colon (`:`) syntax.
+
+ As the type, put `list`.
+
+ As the list is a type that contains some internal types, you put them in square brackets:
+
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/python_types/tutorial006_py39.py!}
+ ```
+
+=== "Python 3.6+"
From `typing`, import `List` (with a capital `L`):
{!> ../../../docs_src/python_types/tutorial006.py!}
```
-=== "Python 3.9 and above"
-
- Declare the variable, with the same colon (`:`) syntax.
-
- As the type, put `list`.
-
- As the list is a type that contains some internal types, you put them in square brackets:
-
- ```Python hl_lines="1"
- {!> ../../../docs_src/python_types/tutorial006_py39.py!}
- ```
-
!!! info
Those internal types in the square brackets are called "type parameters".
You would do the same to declare `tuple`s and `set`s:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial007.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/python_types/tutorial007_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/python_types/tutorial007_py39.py!}
+ ```Python hl_lines="1 4"
+ {!> ../../../docs_src/python_types/tutorial007.py!}
```
This means:
The second type parameter is for the values of the `dict`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial008.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/python_types/tutorial008_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/python_types/tutorial008_py39.py!}
+ ```Python hl_lines="1 4"
+ {!> ../../../docs_src/python_types/tutorial008.py!}
```
This means:
In Python 3.6 and above (including Python 3.10) you can use the `Union` type from `typing` and put inside the square brackets the possible types to accept.
-In Python 3.10 there's also an **alternative syntax** where you can put the possible types separated by a <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr>.
+In Python 3.10 there's also a **new syntax** where you can put the possible types separated by a <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr>.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial008b.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/python_types/tutorial008b_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/python_types/tutorial008b_py310.py!}
+ ```Python hl_lines="1 4"
+ {!> ../../../docs_src/python_types/tutorial008b.py!}
```
In both cases this means that `item` could be an `int` or a `str`.
This also means that in Python 3.10, you can use `Something | None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial009.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/python_types/tutorial009_py310.py!}
```
-=== "Python 3.6 and above - alternative"
+=== "Python 3.6+"
```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial009b.py!}
+ {!> ../../../docs_src/python_types/tutorial009.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+ alternative"
- ```Python hl_lines="1"
- {!> ../../../docs_src/python_types/tutorial009_py310.py!}
+ ```Python hl_lines="1 4"
+ {!> ../../../docs_src/python_types/tutorial009b.py!}
```
#### Using `Union` or `Optional`
These types that take type parameters in square brackets are called **Generic types** or **Generics**, for example:
-=== "Python 3.6 and above"
-
- * `List`
- * `Tuple`
- * `Set`
- * `Dict`
- * `Union`
- * `Optional`
- * ...and others.
-
-=== "Python 3.9 and above"
+=== "Python 3.10+"
You can use the same builtin types as generics (with square brackets and types inside):
And the same as with Python 3.6, from the `typing` module:
* `Union`
- * `Optional`
+ * `Optional` (the same as with Python 3.6)
* ...and others.
-=== "Python 3.10 and above"
+ In Python 3.10, as an alternative to using the generics `Union` and `Optional`, you can use the <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr> to declare unions of types, that's a lot better and simpler.
+
+=== "Python 3.9+"
You can use the same builtin types as generics (with square brackets and types inside):
And the same as with Python 3.6, from the `typing` module:
* `Union`
- * `Optional` (the same as with Python 3.6)
+ * `Optional`
* ...and others.
- In Python 3.10, as an alternative to using the generics `Union` and `Optional`, you can use the <abbr title='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr> to declare unions of types.
+=== "Python 3.6+"
+
+ * `List`
+ * `Tuple`
+ * `Set`
+ * `Dict`
+ * `Union`
+ * `Optional`
+ * ...and others.
### Classes as types
An example from the official Pydantic docs:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- {!> ../../../docs_src/python_types/tutorial011.py!}
+ {!> ../../../docs_src/python_types/tutorial011_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python
{!> ../../../docs_src/python_types/tutorial011_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/python_types/tutorial011_py310.py!}
+ {!> ../../../docs_src/python_types/tutorial011.py!}
```
!!! info
Python also has a feature that allows putting **additional metadata** in these type hints using `Annotated`.
-=== "Python 3.7 and above"
+=== "Python 3.9+"
- In versions below Python 3.9, you import `Annotated` from `typing_extensions`.
-
- It will already be installed with **FastAPI**.
+ In Python 3.9, `Annotated` is part of the standard library, so you can import it from `typing`.
```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial013.py!}
+ {!> ../../../docs_src/python_types/tutorial013_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- In Python 3.9, `Annotated` is part of the standard library, so you can import it from `typing`.
+ In versions below Python 3.9, you import `Annotated` from `typing_extensions`.
+
+ It will already be installed with **FastAPI**.
```Python hl_lines="1 4"
- {!> ../../../docs_src/python_types/tutorial013_py39.py!}
+ {!> ../../../docs_src/python_types/tutorial013.py!}
```
Python itself doesn't do anything with this `Annotated`. And for editors and other tools, the type is still `str`.
**FastAPI** knows what to do in each case and how to re-use the same object, so that all the background tasks are merged together and are run in the background afterwards:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="14 16 23 26"
- {!> ../../../docs_src/background_tasks/tutorial002_an.py!}
+ ```Python hl_lines="13 15 22 25"
+ {!> ../../../docs_src/background_tasks/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="13 15 22 25"
{!> ../../../docs_src/background_tasks/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13 15 22 25"
- {!> ../../../docs_src/background_tasks/tutorial002_an_py310.py!}
+ ```Python hl_lines="14 16 23 26"
+ {!> ../../../docs_src/background_tasks/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="13 15 22 25"
- {!> ../../../docs_src/background_tasks/tutorial002.py!}
+ ```Python hl_lines="11 13 20 23"
+ {!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11 13 20 23"
- {!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
+ ```Python hl_lines="13 15 22 25"
+ {!> ../../../docs_src/background_tasks/tutorial002.py!}
```
In this example, the messages will be written to the `log.txt` file *after* the response is sent.
We will now use a simple dependency to read a custom `X-Token` header:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1 5-7"
- {!> ../../../docs_src/bigger_applications/app_an/dependencies.py!}
+ ```Python hl_lines="3 6-8"
+ {!> ../../../docs_src/bigger_applications/app_an_py39/dependencies.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="3 6-8"
- {!> ../../../docs_src/bigger_applications/app_an_py39/dependencies.py!}
+ ```Python hl_lines="1 5-7"
+ {!> ../../../docs_src/bigger_applications/app_an/dependencies.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
First, you have to import it:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="4"
- {!> ../../../docs_src/body_fields/tutorial001_an.py!}
+ {!> ../../../docs_src/body_fields/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="4"
{!> ../../../docs_src/body_fields/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="4"
- {!> ../../../docs_src/body_fields/tutorial001_an_py310.py!}
+ {!> ../../../docs_src/body_fields/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="4"
- {!> ../../../docs_src/body_fields/tutorial001.py!}
+ ```Python hl_lines="2"
+ {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="2"
- {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
+ ```Python hl_lines="4"
+ {!> ../../../docs_src/body_fields/tutorial001.py!}
```
!!! warning
You can then use `Field` with model attributes:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="12-15"
- {!> ../../../docs_src/body_fields/tutorial001_an.py!}
+ ```Python hl_lines="11-14"
+ {!> ../../../docs_src/body_fields/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11-14"
{!> ../../../docs_src/body_fields/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="11-14"
- {!> ../../../docs_src/body_fields/tutorial001_an_py310.py!}
+ ```Python hl_lines="12-15"
+ {!> ../../../docs_src/body_fields/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11-14"
- {!> ../../../docs_src/body_fields/tutorial001.py!}
+ ```Python hl_lines="9-12"
+ {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9-12"
- {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
+ ```Python hl_lines="11-14"
+ {!> ../../../docs_src/body_fields/tutorial001.py!}
```
`Field` works the same way as `Query`, `Path` and `Body`, it has all the same parameters, etc.
And you can also declare body parameters as optional, by setting the default to `None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="19-21"
- {!> ../../../docs_src/body_multiple_params/tutorial001_an.py!}
+ ```Python hl_lines="18-20"
+ {!> ../../../docs_src/body_multiple_params/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="18-20"
{!> ../../../docs_src/body_multiple_params/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18-20"
- {!> ../../../docs_src/body_multiple_params/tutorial001_an_py310.py!}
+ ```Python hl_lines="19-21"
+ {!> ../../../docs_src/body_multiple_params/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19-21"
- {!> ../../../docs_src/body_multiple_params/tutorial001.py!}
+ ```Python hl_lines="17-19"
+ {!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17-19"
- {!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!}
+ ```Python hl_lines="19-21"
+ {!> ../../../docs_src/body_multiple_params/tutorial001.py!}
```
!!! note
But you can also declare multiple body parameters, e.g. `item` and `user`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="22"
- {!> ../../../docs_src/body_multiple_params/tutorial002.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/body_multiple_params/tutorial002.py!}
```
In this case, **FastAPI** will notice that there are more than one body parameters in the function (two parameters that are Pydantic models).
But you can instruct **FastAPI** to treat it as another body key using `Body`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="24"
- {!> ../../../docs_src/body_multiple_params/tutorial003_an.py!}
+ ```Python hl_lines="23"
+ {!> ../../../docs_src/body_multiple_params/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="23"
{!> ../../../docs_src/body_multiple_params/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="23"
- {!> ../../../docs_src/body_multiple_params/tutorial003_an_py310.py!}
+ ```Python hl_lines="24"
+ {!> ../../../docs_src/body_multiple_params/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="22"
- {!> ../../../docs_src/body_multiple_params/tutorial003.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/body_multiple_params/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="20"
- {!> ../../../docs_src/body_multiple_params/tutorial003_py310.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/body_multiple_params/tutorial003.py!}
```
In this case, **FastAPI** will expect a body like:
For example:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="28"
- {!> ../../../docs_src/body_multiple_params/tutorial004_an.py!}
+ ```Python hl_lines="27"
+ {!> ../../../docs_src/body_multiple_params/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="27"
{!> ../../../docs_src/body_multiple_params/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="27"
- {!> ../../../docs_src/body_multiple_params/tutorial004_an_py310.py!}
+ ```Python hl_lines="28"
+ {!> ../../../docs_src/body_multiple_params/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="27"
- {!> ../../../docs_src/body_multiple_params/tutorial004.py!}
+ ```Python hl_lines="25"
+ {!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="25"
- {!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!}
+ ```Python hl_lines="27"
+ {!> ../../../docs_src/body_multiple_params/tutorial004.py!}
```
!!! info
as in:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/body_multiple_params/tutorial005_an.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/body_multiple_params/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="17"
{!> ../../../docs_src/body_multiple_params/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/body_multiple_params/tutorial005_an_py310.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/body_multiple_params/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17"
- {!> ../../../docs_src/body_multiple_params/tutorial005.py!}
+ ```Python hl_lines="15"
+ {!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="15"
- {!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/body_multiple_params/tutorial005.py!}
```
In this case **FastAPI** will expect a body like:
You can define an attribute to be a subtype. For example, a Python `list`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="14"
- {!> ../../../docs_src/body_nested_models/tutorial001.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/body_nested_models/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/body_nested_models/tutorial001_py310.py!}
+ ```Python hl_lines="14"
+ {!> ../../../docs_src/body_nested_models/tutorial001.py!}
```
This will make `tags` be a list, although it doesn't declare the type of the elements of the list.
So, in our example, we can make `tags` be specifically a "list of strings":
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="14"
- {!> ../../../docs_src/body_nested_models/tutorial002.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/body_nested_models/tutorial002_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial002_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/body_nested_models/tutorial002_py310.py!}
+ ```Python hl_lines="14"
+ {!> ../../../docs_src/body_nested_models/tutorial002.py!}
```
## Set types
Then we can declare `tags` as a set of strings:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="1 14"
- {!> ../../../docs_src/body_nested_models/tutorial003.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/body_nested_models/tutorial003_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial003_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/body_nested_models/tutorial003_py310.py!}
+ ```Python hl_lines="1 14"
+ {!> ../../../docs_src/body_nested_models/tutorial003.py!}
```
With this, even if you receive a request with duplicate data, it will be converted to a set of unique items.
For example, we can define an `Image` model:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9-11"
- {!> ../../../docs_src/body_nested_models/tutorial004.py!}
+ ```Python hl_lines="7-9"
+ {!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9-11"
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7-9"
- {!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
+ ```Python hl_lines="9-11"
+ {!> ../../../docs_src/body_nested_models/tutorial004.py!}
```
### Use the submodel as a type
And then we can use it as the type of an attribute:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/body_nested_models/tutorial004.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/body_nested_models/tutorial004.py!}
```
This would mean that **FastAPI** would expect a body similar to:
For example, as in the `Image` model we have a `url` field, we can declare it to be instead of a `str`, a Pydantic's `HttpUrl`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4 10"
- {!> ../../../docs_src/body_nested_models/tutorial005.py!}
+ ```Python hl_lines="2 8"
+ {!> ../../../docs_src/body_nested_models/tutorial005_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="4 10"
{!> ../../../docs_src/body_nested_models/tutorial005_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="2 8"
- {!> ../../../docs_src/body_nested_models/tutorial005_py310.py!}
+ ```Python hl_lines="4 10"
+ {!> ../../../docs_src/body_nested_models/tutorial005.py!}
```
The string will be checked to be a valid URL, and documented in JSON Schema / OpenAPI as such.
You can also use Pydantic models as subtypes of `list`, `set`, etc:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/body_nested_models/tutorial006.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/body_nested_models/tutorial006_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial006_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/body_nested_models/tutorial006_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/body_nested_models/tutorial006.py!}
```
This will expect (convert, validate, document, etc) a JSON body like:
You can define arbitrarily deeply nested models:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9 14 20 23 27"
- {!> ../../../docs_src/body_nested_models/tutorial007.py!}
+ ```Python hl_lines="7 12 18 21 25"
+ {!> ../../../docs_src/body_nested_models/tutorial007_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9 14 20 23 27"
{!> ../../../docs_src/body_nested_models/tutorial007_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7 12 18 21 25"
- {!> ../../../docs_src/body_nested_models/tutorial007_py310.py!}
+ ```Python hl_lines="9 14 20 23 27"
+ {!> ../../../docs_src/body_nested_models/tutorial007.py!}
```
!!! info
as in:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="15"
- {!> ../../../docs_src/body_nested_models/tutorial008.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/body_nested_models/tutorial008_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13"
- {!> ../../../docs_src/body_nested_models/tutorial008_py39.py!}
+ ```Python hl_lines="15"
+ {!> ../../../docs_src/body_nested_models/tutorial008.py!}
```
## Editor support everywhere
In this case, you would accept any `dict` as long as it has `int` keys with `float` values:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/body_nested_models/tutorial009.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/body_nested_models/tutorial009_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/body_nested_models/tutorial009_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/body_nested_models/tutorial009.py!}
```
!!! tip
You can use the `jsonable_encoder` to convert the input data to data that can be stored as JSON (e.g. with a NoSQL database). For example, converting `datetime` to `str`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="30-35"
- {!> ../../../docs_src/body_updates/tutorial001.py!}
+ ```Python hl_lines="28-33"
+ {!> ../../../docs_src/body_updates/tutorial001_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="30-35"
{!> ../../../docs_src/body_updates/tutorial001_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="28-33"
- {!> ../../../docs_src/body_updates/tutorial001_py310.py!}
+ ```Python hl_lines="30-35"
+ {!> ../../../docs_src/body_updates/tutorial001.py!}
```
`PUT` is used to receive data that should replace the existing data.
Then you can use this to generate a `dict` with only the data that was set (sent in the request), omitting default values:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="34"
- {!> ../../../docs_src/body_updates/tutorial002.py!}
+ ```Python hl_lines="32"
+ {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="34"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="32"
- {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
+ ```Python hl_lines="34"
+ {!> ../../../docs_src/body_updates/tutorial002.py!}
```
### Using Pydantic's `update` parameter
Like `stored_item_model.copy(update=update_data)`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="35"
- {!> ../../../docs_src/body_updates/tutorial002.py!}
+ ```Python hl_lines="33"
+ {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="35"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="33"
- {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
+ ```Python hl_lines="35"
+ {!> ../../../docs_src/body_updates/tutorial002.py!}
```
### Partial updates recap
* Save the data to your DB.
* Return the updated model.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="30-37"
- {!> ../../../docs_src/body_updates/tutorial002.py!}
+ ```Python hl_lines="28-35"
+ {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="30-37"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="28-35"
- {!> ../../../docs_src/body_updates/tutorial002_py310.py!}
+ ```Python hl_lines="30-37"
+ {!> ../../../docs_src/body_updates/tutorial002.py!}
```
!!! tip
First, you need to import `BaseModel` from `pydantic`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4"
- {!> ../../../docs_src/body/tutorial001.py!}
+ ```Python hl_lines="2"
+ {!> ../../../docs_src/body/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="2"
- {!> ../../../docs_src/body/tutorial001_py310.py!}
+ ```Python hl_lines="4"
+ {!> ../../../docs_src/body/tutorial001.py!}
```
## Create your data model
Use standard Python types for all the attributes:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="7-11"
- {!> ../../../docs_src/body/tutorial001.py!}
+ ```Python hl_lines="5-9"
+ {!> ../../../docs_src/body/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="5-9"
- {!> ../../../docs_src/body/tutorial001_py310.py!}
+ ```Python hl_lines="7-11"
+ {!> ../../../docs_src/body/tutorial001.py!}
```
The same as when declaring query parameters, when a model attribute has a default value, it is not required. Otherwise, it is required. Use `None` to make it just optional.
To add it to your *path operation*, declare it the same way you declared path and query parameters:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/body/tutorial001.py!}
+ ```Python hl_lines="16"
+ {!> ../../../docs_src/body/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="16"
- {!> ../../../docs_src/body/tutorial001_py310.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/body/tutorial001.py!}
```
...and declare its type as the model you created, `Item`.
Inside of the function, you can access all the attributes of the model object directly:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="21"
- {!> ../../../docs_src/body/tutorial002.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/body/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/body/tutorial002_py310.py!}
+ ```Python hl_lines="21"
+ {!> ../../../docs_src/body/tutorial002.py!}
```
## Request body + path parameters
**FastAPI** will recognize that the function parameters that match path parameters should be **taken from the path**, and that function parameters that are declared to be Pydantic models should be **taken from the request body**.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="17-18"
- {!> ../../../docs_src/body/tutorial003.py!}
+ ```Python hl_lines="15-16"
+ {!> ../../../docs_src/body/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="15-16"
- {!> ../../../docs_src/body/tutorial003_py310.py!}
+ ```Python hl_lines="17-18"
+ {!> ../../../docs_src/body/tutorial003.py!}
```
## Request body + path + query parameters
**FastAPI** will recognize each of them and take the data from the correct place.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/body/tutorial004.py!}
+ ```Python hl_lines="16"
+ {!> ../../../docs_src/body/tutorial004_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="16"
- {!> ../../../docs_src/body/tutorial004_py310.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/body/tutorial004.py!}
```
The function parameters will be recognized as follows:
First import `Cookie`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="3"
- {!> ../../../docs_src/cookie_params/tutorial001_an.py!}
+ {!> ../../../docs_src/cookie_params/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3"
{!> ../../../docs_src/cookie_params/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="3"
- {!> ../../../docs_src/cookie_params/tutorial001_an_py310.py!}
+ {!> ../../../docs_src/cookie_params/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3"
- {!> ../../../docs_src/cookie_params/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1"
- {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/cookie_params/tutorial001.py!}
```
## Declare `Cookie` parameters
The first value is the default value, you can pass all the extra validation or annotation parameters:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/cookie_params/tutorial001_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/cookie_params/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/cookie_params/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/cookie_params/tutorial001_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/cookie_params/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/cookie_params/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/cookie_params/tutorial001.py!}
```
!!! note "Technical Details"
In the previous example, we were returning a `dict` from our dependency ("dependable"):
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/dependencies/tutorial001_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
But then we get a `dict` in the parameter `commons` of the *path operation function*.
Then, we can change the dependency "dependable" `common_parameters` from above to the class `CommonQueryParams`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="12-16"
- {!> ../../../docs_src/dependencies/tutorial002_an.py!}
+ ```Python hl_lines="11-15"
+ {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11-15"
{!> ../../../docs_src/dependencies/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="11-15"
- {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
+ ```Python hl_lines="12-16"
+ {!> ../../../docs_src/dependencies/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11-15"
- {!> ../../../docs_src/dependencies/tutorial002.py!}
+ ```Python hl_lines="9-13"
+ {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9-13"
- {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
+ ```Python hl_lines="11-15"
+ {!> ../../../docs_src/dependencies/tutorial002.py!}
```
Pay attention to the `__init__` method used to create the instance of the class:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="13"
- {!> ../../../docs_src/dependencies/tutorial002_an.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="12"
{!> ../../../docs_src/dependencies/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/dependencies/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="12"
- {!> ../../../docs_src/dependencies/tutorial002.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/dependencies/tutorial002.py!}
```
...it has the same parameters as our previous `common_parameters`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/dependencies/tutorial001_an.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="6"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="6"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
Those parameters are what **FastAPI** will use to "solve" the dependency.
Now you can declare your dependency using this class.
+=== "Python 3.10+"
-=== "Python 3.6 and above"
-
- ```Python hl_lines="20"
- {!> ../../../docs_src/dependencies/tutorial002_an.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial002_an_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/dependencies/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial002.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial002.py!}
```
**FastAPI** calls the `CommonQueryParams` class. This creates an "instance" of that class and the instance will be passed as the parameter `commons` to your function.
Notice how we write `CommonQueryParams` twice in the above code:
-=== "Python 3.6 and above"
-
- ```Python
- commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
- ```
-
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
commons: CommonQueryParams = Depends(CommonQueryParams)
```
+=== "Python 3.6+"
+
+ ```Python
+ commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
+ ```
+
The last `CommonQueryParams`, in:
```Python
In this case, the first `CommonQueryParams`, in:
-=== "Python 3.6 and above"
+=== "Python 3.6+"
```Python
commons: Annotated[CommonQueryParams, ...
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
You could actually write just:
-=== "Python 3.6 and above"
+=== "Python 3.6+"
```Python
commons: Annotated[Any, Depends(CommonQueryParams)]
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
..as in:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/dependencies/tutorial003_an.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial003_an_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/dependencies/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial003.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial003_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial003.py!}
```
But declaring the type is encouraged as that way your editor will know what will be passed as the parameter `commons`, and then it can help you with code completion, type checks, etc:
But you see that we are having some code repetition here, writing `CommonQueryParams` twice:
-=== "Python 3.6 and above"
-
- ```Python
- commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
- ```
-
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
commons: CommonQueryParams = Depends(CommonQueryParams)
```
+=== "Python 3.6+"
+
+ ```Python
+ commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
+ ```
+
**FastAPI** provides a shortcut for these cases, in where the dependency is *specifically* a class that **FastAPI** will "call" to create an instance of the class itself.
For those specific cases, you can do the following:
Instead of writing:
-=== "Python 3.6 and above"
+=== "Python 3.6+"
```Python
commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
...you write:
-=== "Python 3.6 and above"
+=== "Python 3.6+"
```Python
commons: Annotated[CommonQueryParams, Depends()]
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
The same example would then look like:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/dependencies/tutorial004_an.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial004_an_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/dependencies/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial004.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial004_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial004.py!}
```
...and **FastAPI** will know what to do.
It should be a `list` of `Depends()`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/dependencies/tutorial006_an.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/dependencies/tutorial006_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
They can declare request requirements (like headers) or other sub-dependencies:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="7 12"
- {!> ../../../docs_src/dependencies/tutorial006_an.py!}
+ ```Python hl_lines="8 13"
+ {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8 13"
- {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
+ ```Python hl_lines="7 12"
+ {!> ../../../docs_src/dependencies/tutorial006_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
These dependencies can `raise` exceptions, the same as normal dependencies:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9 14"
- {!> ../../../docs_src/dependencies/tutorial006_an.py!}
+ ```Python hl_lines="10 15"
+ {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10 15"
- {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
+ ```Python hl_lines="9 14"
+ {!> ../../../docs_src/dependencies/tutorial006_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
So, you can re-use a normal dependency (that returns a value) you already use somewhere else, and even though the value won't be used, the dependency will be executed:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="10 15"
- {!> ../../../docs_src/dependencies/tutorial006_an.py!}
+ ```Python hl_lines="11 16"
+ {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="11 16"
- {!> ../../../docs_src/dependencies/tutorial006_an_py39.py!}
+ ```Python hl_lines="10 15"
+ {!> ../../../docs_src/dependencies/tutorial006_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
For example, `dependency_c` can have a dependency on `dependency_b`, and `dependency_b` on `dependency_a`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="5 13 21"
- {!> ../../../docs_src/dependencies/tutorial008_an.py!}
+ ```Python hl_lines="6 14 22"
+ {!> ../../../docs_src/dependencies/tutorial008_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6 14 22"
- {!> ../../../docs_src/dependencies/tutorial008_an_py39.py!}
+ ```Python hl_lines="5 13 21"
+ {!> ../../../docs_src/dependencies/tutorial008_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
And, in turn, `dependency_b` needs the value from `dependency_a` (here named `dep_a`) to be available for its exit code.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="17-18 25-26"
- {!> ../../../docs_src/dependencies/tutorial008_an.py!}
+ ```Python hl_lines="18-19 26-27"
+ {!> ../../../docs_src/dependencies/tutorial008_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18-19 26-27"
- {!> ../../../docs_src/dependencies/tutorial008_an_py39.py!}
+ ```Python hl_lines="17-18 25-26"
+ {!> ../../../docs_src/dependencies/tutorial008_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
In that case, they will be applied to all the *path operations* in the application:
-
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python hl_lines="16"
- {!> ../../../docs_src/dependencies/tutorial012_an.py!}
+ {!> ../../../docs_src/dependencies/tutorial012_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python hl_lines="16"
- {!> ../../../docs_src/dependencies/tutorial012_an_py39.py!}
+ {!> ../../../docs_src/dependencies/tutorial012_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
It is just a function that can take all the same parameters that a *path operation function* can take:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9-12"
- {!> ../../../docs_src/dependencies/tutorial001_an.py!}
+ ```Python hl_lines="8-9"
+ {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="8-11"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8-9"
- {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
+ ```Python hl_lines="9-12"
+ {!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8-11"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="6-7"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="6-7"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="8-11"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
That's it.
### Import `Depends`
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="3"
- {!> ../../../docs_src/dependencies/tutorial001_an.py!}
+ {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="3"
- {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
+ {!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
### Declare the dependency, in the "dependant"
The same way you use `Body`, `Query`, etc. with your *path operation function* parameters, use `Depends` with a new parameter:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="16 21"
- {!> ../../../docs_src/dependencies/tutorial001_an.py!}
+ ```Python hl_lines="13 18"
+ {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="15 20"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13 18"
- {!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
+ ```Python hl_lines="16 21"
+ {!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="15 20"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="11 16"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11 16"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="15 20"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
Although you use `Depends` in the parameters of your function the same way you use `Body`, `Query`, etc, `Depends` works a bit differently.
But because we are using `Annotated`, we can store that `Annotated` value in a variable and use it in multiple places:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="15 19 24"
- {!> ../../../docs_src/dependencies/tutorial001_02_an.py!}
+ ```Python hl_lines="12 16 21"
+ {!> ../../../docs_src/dependencies/tutorial001_02_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="14 18 23"
{!> ../../../docs_src/dependencies/tutorial001_02_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12 16 21"
- {!> ../../../docs_src/dependencies/tutorial001_02_an_py310.py!}
+ ```Python hl_lines="15 19 24"
+ {!> ../../../docs_src/dependencies/tutorial001_02_an.py!}
```
!!! tip
You could create a first dependency ("dependable") like:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9-10"
- {!> ../../../docs_src/dependencies/tutorial005_an.py!}
+ ```Python hl_lines="8-9"
+ {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="8-9"
{!> ../../../docs_src/dependencies/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8-9"
- {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
+ ```Python hl_lines="9-10"
+ {!> ../../../docs_src/dependencies/tutorial005_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.10 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8-9"
- {!> ../../../docs_src/dependencies/tutorial005.py!}
+ ```Python hl_lines="6-7"
+ {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
-=== "Python 3.10 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="6-7"
- {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
+ ```Python hl_lines="8-9"
+ {!> ../../../docs_src/dependencies/tutorial005.py!}
```
It declares an optional query parameter `q` as a `str`, and then it just returns it.
Then you can create another dependency function (a "dependable") that at the same time declares a dependency of its own (so it is a "dependant" too):
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="14"
- {!> ../../../docs_src/dependencies/tutorial005_an.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="13"
{!> ../../../docs_src/dependencies/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13"
- {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
+ ```Python hl_lines="14"
+ {!> ../../../docs_src/dependencies/tutorial005_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.10 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="13"
- {!> ../../../docs_src/dependencies/tutorial005.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
-=== "Python 3.10 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11"
- {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/dependencies/tutorial005.py!}
```
Let's focus on the parameters declared:
Then we can use the dependency with:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="24"
- {!> ../../../docs_src/dependencies/tutorial005_an.py!}
+ ```Python hl_lines="23"
+ {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="23"
{!> ../../../docs_src/dependencies/tutorial005_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="23"
- {!> ../../../docs_src/dependencies/tutorial005_an_py310.py!}
+ ```Python hl_lines="24"
+ {!> ../../../docs_src/dependencies/tutorial005_an.py!}
```
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.10 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="22"
- {!> ../../../docs_src/dependencies/tutorial005.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
-=== "Python 3.10 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial005_py310.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/dependencies/tutorial005.py!}
```
!!! info
In an advanced scenario where you know you need the dependency to be called at every step (possibly multiple times) in the same request instead of using the "cached" value, you can set the parameter `use_cache=False` when using `Depends`:
-=== "Python 3.6 and above"
+=== "Python 3.6+"
```Python hl_lines="1"
async def needy_dependency(fresh_value: Annotated[str, Depends(get_value, use_cache=False)]):
return {"fresh_value": fresh_value}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
It receives an object, like a Pydantic model, and returns a JSON compatible version:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="5 22"
- {!> ../../../docs_src/encoder/tutorial001.py!}
+ ```Python hl_lines="4 21"
+ {!> ../../../docs_src/encoder/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 21"
- {!> ../../../docs_src/encoder/tutorial001_py310.py!}
+ ```Python hl_lines="5 22"
+ {!> ../../../docs_src/encoder/tutorial001.py!}
```
In this example, it would convert the Pydantic model to a `dict`, and the `datetime` to a `str`.
Here's an example *path operation* with parameters using some of the above types.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="1 3 13-17"
- {!> ../../../docs_src/extra_data_types/tutorial001_an.py!}
+ ```Python hl_lines="1 3 12-16"
+ {!> ../../../docs_src/extra_data_types/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="1 3 12-16"
{!> ../../../docs_src/extra_data_types/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1 3 12-16"
- {!> ../../../docs_src/extra_data_types/tutorial001_an_py310.py!}
+ ```Python hl_lines="1 3 13-17"
+ {!> ../../../docs_src/extra_data_types/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1 2 12-16"
- {!> ../../../docs_src/extra_data_types/tutorial001.py!}
+ ```Python hl_lines="1 2 11-15"
+ {!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1 2 11-15"
- {!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
+ ```Python hl_lines="1 2 12-16"
+ {!> ../../../docs_src/extra_data_types/tutorial001.py!}
```
Note that the parameters inside the function have their natural data type, and you can, for example, perform normal date manipulations, like:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="19-20"
- {!> ../../../docs_src/extra_data_types/tutorial001_an.py!}
+ ```Python hl_lines="18-19"
+ {!> ../../../docs_src/extra_data_types/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="18-19"
{!> ../../../docs_src/extra_data_types/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18-19"
- {!> ../../../docs_src/extra_data_types/tutorial001_an_py310.py!}
+ ```Python hl_lines="19-20"
+ {!> ../../../docs_src/extra_data_types/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="18-19"
- {!> ../../../docs_src/extra_data_types/tutorial001.py!}
+ ```Python hl_lines="17-18"
+ {!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17-18"
- {!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
+ ```Python hl_lines="18-19"
+ {!> ../../../docs_src/extra_data_types/tutorial001.py!}
```
Here's a general idea of how the models could look like with their password fields and the places where they are used:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9 11 16 22 24 29-30 33-35 40-41"
- {!> ../../../docs_src/extra_models/tutorial001.py!}
+ ```Python hl_lines="7 9 14 20 22 27-28 31-33 38-39"
+ {!> ../../../docs_src/extra_models/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7 9 14 20 22 27-28 31-33 38-39"
- {!> ../../../docs_src/extra_models/tutorial001_py310.py!}
+ ```Python hl_lines="9 11 16 22 24 29-30 33-35 40-41"
+ {!> ../../../docs_src/extra_models/tutorial001.py!}
```
### About `**user_in.dict()`
That way, we can declare just the differences between the models (with plaintext `password`, with `hashed_password` and without password):
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9 15-16 19-20 23-24"
- {!> ../../../docs_src/extra_models/tutorial002.py!}
+ ```Python hl_lines="7 13-14 17-18 21-22"
+ {!> ../../../docs_src/extra_models/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7 13-14 17-18 21-22"
- {!> ../../../docs_src/extra_models/tutorial002_py310.py!}
+ ```Python hl_lines="9 15-16 19-20 23-24"
+ {!> ../../../docs_src/extra_models/tutorial002.py!}
```
## `Union` or `anyOf`
!!! note
When defining a <a href="https://pydantic-docs.helpmanual.io/usage/types/#unions" class="external-link" target="_blank">`Union`</a>, include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `Union[PlaneItem, CarItem]`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="1 14-15 18-20 33"
- {!> ../../../docs_src/extra_models/tutorial003.py!}
+ {!> ../../../docs_src/extra_models/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="1 14-15 18-20 33"
- {!> ../../../docs_src/extra_models/tutorial003_py310.py!}
+ {!> ../../../docs_src/extra_models/tutorial003.py!}
```
### `Union` in Python 3.10
For that, use the standard Python `typing.List` (or just `list` in Python 3.9 and above):
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1 20"
- {!> ../../../docs_src/extra_models/tutorial004.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/extra_models/tutorial004_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/extra_models/tutorial004_py39.py!}
+ ```Python hl_lines="1 20"
+ {!> ../../../docs_src/extra_models/tutorial004.py!}
```
## Response with arbitrary `dict`
In this case, you can use `typing.Dict` (or just `dict` in Python 3.9 and above):
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1 8"
- {!> ../../../docs_src/extra_models/tutorial005.py!}
+ ```Python hl_lines="6"
+ {!> ../../../docs_src/extra_models/tutorial005_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6"
- {!> ../../../docs_src/extra_models/tutorial005_py39.py!}
+ ```Python hl_lines="1 8"
+ {!> ../../../docs_src/extra_models/tutorial005.py!}
```
## Recap
First import `Header`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="3"
- {!> ../../../docs_src/header_params/tutorial001_an.py!}
+ {!> ../../../docs_src/header_params/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3"
{!> ../../../docs_src/header_params/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="3"
- {!> ../../../docs_src/header_params/tutorial001_an_py310.py!}
+ {!> ../../../docs_src/header_params/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3"
- {!> ../../../docs_src/header_params/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1"
- {!> ../../../docs_src/header_params/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/header_params/tutorial001.py!}
```
## Declare `Header` parameters
The first value is the default value, you can pass all the extra validation or annotation parameters:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/header_params/tutorial001_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial001_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/header_params/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/header_params/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial001.py!}
```
!!! note "Technical Details"
If for some reason you need to disable automatic conversion of underscores to hyphens, set the parameter `convert_underscores` of `Header` to `False`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/header_params/tutorial002_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/header_params/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11"
{!> ../../../docs_src/header_params/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/header_params/tutorial002_an_py310.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/header_params/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/header_params/tutorial002.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/header_params/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8"
- {!> ../../../docs_src/header_params/tutorial002_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/header_params/tutorial002.py!}
```
!!! warning
For example, to declare a header of `X-Token` that can appear more than once, you can write:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/header_params/tutorial003_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial003_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/header_params/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial003.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/header_params/tutorial003_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/header_params/tutorial003_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/header_params/tutorial003_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial003.py!}
```
If you communicate with that *path operation* sending two HTTP headers like:
But if you don't remember what each number code is for, you can use the shortcut constants in `status`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="3 17"
- {!> ../../../docs_src/path_operation_configuration/tutorial001.py!}
+ ```Python hl_lines="1 15"
+ {!> ../../../docs_src/path_operation_configuration/tutorial001_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3 17"
{!> ../../../docs_src/path_operation_configuration/tutorial001_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1 15"
- {!> ../../../docs_src/path_operation_configuration/tutorial001_py310.py!}
+ ```Python hl_lines="3 17"
+ {!> ../../../docs_src/path_operation_configuration/tutorial001.py!}
```
That status code will be used in the response and will be added to the OpenAPI schema.
You can add tags to your *path operation*, pass the parameter `tags` with a `list` of `str` (commonly just one `str`):
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="17 22 27"
- {!> ../../../docs_src/path_operation_configuration/tutorial002.py!}
+ ```Python hl_lines="15 20 25"
+ {!> ../../../docs_src/path_operation_configuration/tutorial002_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="17 22 27"
{!> ../../../docs_src/path_operation_configuration/tutorial002_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="15 20 25"
- {!> ../../../docs_src/path_operation_configuration/tutorial002_py310.py!}
+ ```Python hl_lines="17 22 27"
+ {!> ../../../docs_src/path_operation_configuration/tutorial002.py!}
```
They will be added to the OpenAPI schema and used by the automatic documentation interfaces:
You can add a `summary` and `description`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20-21"
- {!> ../../../docs_src/path_operation_configuration/tutorial003.py!}
+ ```Python hl_lines="18-19"
+ {!> ../../../docs_src/path_operation_configuration/tutorial003_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="20-21"
{!> ../../../docs_src/path_operation_configuration/tutorial003_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="18-19"
- {!> ../../../docs_src/path_operation_configuration/tutorial003_py310.py!}
+ ```Python hl_lines="20-21"
+ {!> ../../../docs_src/path_operation_configuration/tutorial003.py!}
```
## Description from docstring
You can write <a href="https://en.wikipedia.org/wiki/Markdown" class="external-link" target="_blank">Markdown</a> in the docstring, it will be interpreted and displayed correctly (taking into account docstring indentation).
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="19-27"
- {!> ../../../docs_src/path_operation_configuration/tutorial004.py!}
+ ```Python hl_lines="17-25"
+ {!> ../../../docs_src/path_operation_configuration/tutorial004_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19-27"
{!> ../../../docs_src/path_operation_configuration/tutorial004_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="17-25"
- {!> ../../../docs_src/path_operation_configuration/tutorial004_py310.py!}
+ ```Python hl_lines="19-27"
+ {!> ../../../docs_src/path_operation_configuration/tutorial004.py!}
```
It will be used in the interactive docs:
You can specify the response description with the parameter `response_description`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="21"
- {!> ../../../docs_src/path_operation_configuration/tutorial005.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/path_operation_configuration/tutorial005_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="21"
{!> ../../../docs_src/path_operation_configuration/tutorial005_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/path_operation_configuration/tutorial005_py310.py!}
+ ```Python hl_lines="21"
+ {!> ../../../docs_src/path_operation_configuration/tutorial005.py!}
```
!!! info
First, import `Path` from `fastapi`, and import `Annotated`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="3-4"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an.py!}
+ ```Python hl_lines="1 3"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="1 3"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1 3"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py!}
+ ```Python hl_lines="3-4"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
## Declare metadata
For example, to declare a `title` metadata value for the path parameter `item_id` you can type:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="10"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
!!! note
So, you can declare your function as:
-=== "Python 3.6 - non-Annotated"
+=== "Python 3.6 non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
But have 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()`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial002_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial002_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial002_an_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial002_an.py!}
```
## Order the parameters as you need, tricks
Have in mind that if you use `Annotated`, as you are not using function parameter default values, you won't have this problem, and yo probably won't need to use `*`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial003_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial003_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial003_an_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial003_an.py!}
```
## Number validations: greater than or equal
Here, with `ge=1`, `item_id` will need to be an integer number "`g`reater than or `e`qual" to `1`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial004_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial004_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial004_an_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
* `gt`: `g`reater `t`han
* `le`: `l`ess than or `e`qual
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial005_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial005_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial005_an_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial005_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
And the same for <abbr title="less than"><code>lt</code></abbr>.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial006_an.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial006_an_py39.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial006_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Let's take this application as example:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial001.py!}
```
The query parameter `q` is of type `Union[str, None]` (or `str | None` in Python 3.10), that means that it's of type `str` but could also be `None`, and indeed, the default value is `None`, so FastAPI will know it's not required.
* `Query` from `fastapi`
* `Annotated` from `typing` (or from `typing_extensions` in Python below 3.9)
-=== "Python 3.6 and above"
+=== "Python 3.10+"
+
+ In Python 3.9 or above, `Annotated` is part of the standard library, so you can import it from `typing`.
+
+ ```Python hl_lines="1 3"
+ {!> ../../../docs_src/query_params_str_validations/tutorial002_an_py310.py!}
+ ```
+
+=== "Python 3.6+"
In versions of Python below Python 3.9 you import `Annotation` from `typing_extensions`.
{!> ../../../docs_src/query_params_str_validations/tutorial002_an.py!}
```
-=== "Python 3.10 and above"
-
- In Python 3.9 or above, `Annotated` is part of the standard library, so you can import it from `typing`.
-
- ```Python hl_lines="1 3"
- {!> ../../../docs_src/query_params_str_validations/tutorial002_an_py310.py!}
- ```
-
## Use `Annotated` in the type for the `q` parameter
Remember I told you before that `Annotated` can be used to add metadata to your parameters in the [Python Types Intro](../python-types.md#type-hints-with-metadata-annotations){.internal-link target=_blank}?
We had this type annotation:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- q: Union[str, None] = None
+ q: str | None = None
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- q: str | None = None
+ q: Union[str, None] = None
```
What we will do is wrap that with `Annotated`, so it becomes:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- q: Annotated[Union[str, None]] = None
+ q: Annotated[str | None] = None
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- q: Annotated[str | None] = None
+ q: Annotated[Union[str, None]] = None
```
Both of those versions mean the same thing, `q` is a parameter that can be a `str` or `None`, and by default, it is `None`.
Now that we have this `Annotated` where we can put more metadata, add `Query` to it, and set the parameter `max_length` to 50:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial002_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial002_an_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial002_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial002_an.py!}
```
Notice that the default value is still `None`, so the parameter is still optional.
This is how you would use `Query()` as the default value of your function parameter, setting the parameter `max_length` to 50:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
```
As in this case (without using `Annotated`) we have to replace the default value `None` in the function with `Query()`, we now need to set the default value with the parameter `Query(default=None)`, it serves the same purpose of defining that default value (at least for FastAPI).
You can also add a parameter `min_length`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/query_params_str_validations/tutorial003_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial003_an_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/query_params_str_validations/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial003.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial003_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial003.py!}
```
## Add regular expressions
You can define a <abbr title="A regular expression, regex or regexp is a sequence of characters that define a search pattern for strings.">regular expression</abbr> that the parameter should match:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/query_params_str_validations/tutorial004_an.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/query_params_str_validations/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11"
{!> ../../../docs_src/query_params_str_validations/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/query_params_str_validations/tutorial004_an_py310.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/query_params_str_validations/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11"
- {!> ../../../docs_src/query_params_str_validations/tutorial004.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial004_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/query_params_str_validations/tutorial004.py!}
```
This specific regular expression checks that the received parameter value:
Let's say that you want to declare the `q` query parameter to have a `min_length` of `3`, and to have a default value of `"fixedquery"`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial005_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial005_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial005_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial005_an.py!}
```
-=== "non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
So, when you need to declare a value as required while using `Query`, you can simply not declare a default value:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial006_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial006_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
There's an alternative way to explicitly declare that a value is required. You can set the default to the literal value `...`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial006b_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006b_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial006b_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006b_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
To do that, you can declare that `None` is a valid type but still use `...` as the default:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial006c_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006c_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial006c_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial006c_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006c_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial006c.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006c_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial006c_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006c.py!}
```
!!! tip
If you feel uncomfortable using `...`, you can also import and use `Required` from Pydantic:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="2 9"
- {!> ../../../docs_src/query_params_str_validations/tutorial006d_an.py!}
+ ```Python hl_lines="4 10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006d_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 10"
- {!> ../../../docs_src/query_params_str_validations/tutorial006d_an_py39.py!}
+ ```Python hl_lines="2 9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial006d_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
For example, to declare a query parameter `q` that can appear multiple times in the URL, you can write:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial011_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial011_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial011_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial011_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial011_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial011.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial011_py310.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/query_params_str_validations/tutorial011_py39.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial011_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial011.py!}
```
Then, with a URL like:
And you can also define a default `list` of values if none are provided:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial012_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial012_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial012_an_py39.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial012_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial012.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial012_py39.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial012_py39.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial012.py!}
```
If you go to:
You can also use `list` directly instead of `List[str]` (or `list[str]` in Python 3.9+):
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial013_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial013_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial013_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial013_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
You can add a `title`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/query_params_str_validations/tutorial007_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial007_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial007_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial007_an_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/query_params_str_validations/tutorial007_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial007.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial007_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial007_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial007.py!}
```
-
And a `description`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="15"
- {!> ../../../docs_src/query_params_str_validations/tutorial008_an.py!}
+ ```Python hl_lines="14"
+ {!> ../../../docs_src/query_params_str_validations/tutorial008_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="14"
{!> ../../../docs_src/query_params_str_validations/tutorial008_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="14"
- {!> ../../../docs_src/query_params_str_validations/tutorial008_an_py310.py!}
+ ```Python hl_lines="15"
+ {!> ../../../docs_src/query_params_str_validations/tutorial008_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="13"
- {!> ../../../docs_src/query_params_str_validations/tutorial008.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="12"
- {!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/query_params_str_validations/tutorial008.py!}
```
## Alias parameters
Then you can declare an `alias`, and that alias is what will be used to find the parameter value:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial009_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial009_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial009_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial009_an_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial009_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params_str_validations/tutorial009.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params_str_validations/tutorial009_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params_str_validations/tutorial009_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params_str_validations/tutorial009.py!}
```
## Deprecating parameters
Then pass the parameter `deprecated=True` to `Query`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/query_params_str_validations/tutorial010_an.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/query_params_str_validations/tutorial010_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19"
{!> ../../../docs_src/query_params_str_validations/tutorial010_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/query_params_str_validations/tutorial010_an_py310.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/query_params_str_validations/tutorial010_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="18"
- {!> ../../../docs_src/query_params_str_validations/tutorial010.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17"
- {!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/query_params_str_validations/tutorial010.py!}
```
The docs will show it like this:
To exclude a query parameter from the generated OpenAPI schema (and thus, from the automatic documentation systems), set the parameter `include_in_schema` of `Query` to `False`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/query_params_str_validations/tutorial014_an.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial014_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial014_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial014_an_py310.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/query_params_str_validations/tutorial014_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params_str_validations/tutorial014.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params_str_validations/tutorial014.py!}
```
## Recap
The same way, you can declare optional query parameters, by setting their default to `None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params/tutorial002.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params/tutorial002_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params/tutorial002.py!}
```
In this case, the function parameter `q` will be optional, and will be `None` by default.
You can also declare `bool` types, and they will be converted:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params/tutorial003.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params/tutorial003_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params/tutorial003.py!}
```
In this case, if you go to:
They will be detected by name:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="8 10"
- {!> ../../../docs_src/query_params/tutorial004.py!}
+ ```Python hl_lines="6 8"
+ {!> ../../../docs_src/query_params/tutorial004_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6 8"
- {!> ../../../docs_src/query_params/tutorial004_py310.py!}
+ ```Python hl_lines="8 10"
+ {!> ../../../docs_src/query_params/tutorial004.py!}
```
## Required query parameters
And of course, you can define some parameters as required, some as having a default value, and some entirely optional:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params/tutorial006.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params/tutorial006_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params/tutorial006_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params/tutorial006.py!}
```
In this case, there are 3 query parameters:
Import `File` and `UploadFile` from `fastapi`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/request_files/tutorial001_an.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/request_files/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Create file parameters the same way you would for `Body` or `Form`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/request_files/tutorial001_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/request_files/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Define a file parameter with a type of `UploadFile`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="13"
- {!> ../../../docs_src/request_files/tutorial001_an.py!}
+ ```Python hl_lines="14"
+ {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="14"
- {!> ../../../docs_src/request_files/tutorial001_an_py39.py!}
+ ```Python hl_lines="13"
+ {!> ../../../docs_src/request_files/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
You can make a file optional by using standard type annotations and setting a default value of `None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10 18"
- {!> ../../../docs_src/request_files/tutorial001_02_an.py!}
+ ```Python hl_lines="9 17"
+ {!> ../../../docs_src/request_files/tutorial001_02_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9 17"
{!> ../../../docs_src/request_files/tutorial001_02_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9 17"
- {!> ../../../docs_src/request_files/tutorial001_02_an_py310.py!}
+ ```Python hl_lines="10 18"
+ {!> ../../../docs_src/request_files/tutorial001_02_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9 17"
- {!> ../../../docs_src/request_files/tutorial001_02.py!}
+ ```Python hl_lines="7 15"
+ {!> ../../../docs_src/request_files/tutorial001_02_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7 15"
- {!> ../../../docs_src/request_files/tutorial001_02_py310.py!}
+ ```Python hl_lines="9 17"
+ {!> ../../../docs_src/request_files/tutorial001_02.py!}
```
-
## `UploadFile` with Additional Metadata
You can also use `File()` with `UploadFile`, for example, to set additional metadata:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8 14"
- {!> ../../../docs_src/request_files/tutorial001_03_an.py!}
+ ```Python hl_lines="9 15"
+ {!> ../../../docs_src/request_files/tutorial001_03_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9 15"
- {!> ../../../docs_src/request_files/tutorial001_03_an_py39.py!}
+ ```Python hl_lines="8 14"
+ {!> ../../../docs_src/request_files/tutorial001_03_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
To use that, declare a list of `bytes` or `UploadFile`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="11 16"
- {!> ../../../docs_src/request_files/tutorial002_an.py!}
+ ```Python hl_lines="10 15"
+ {!> ../../../docs_src/request_files/tutorial002_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10 15"
- {!> ../../../docs_src/request_files/tutorial002_an_py39.py!}
+ ```Python hl_lines="11 16"
+ {!> ../../../docs_src/request_files/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="10 15"
- {!> ../../../docs_src/request_files/tutorial002.py!}
+ ```Python hl_lines="8 13"
+ {!> ../../../docs_src/request_files/tutorial002_py39.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="8 13"
- {!> ../../../docs_src/request_files/tutorial002_py39.py!}
+ ```Python hl_lines="10 15"
+ {!> ../../../docs_src/request_files/tutorial002.py!}
```
You will receive, as declared, a `list` of `bytes` or `UploadFile`s.
And the same way as before, you can use `File()` to set additional parameters, even for `UploadFile`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="12 19-21"
- {!> ../../../docs_src/request_files/tutorial003_an.py!}
+ ```Python hl_lines="11 18-20"
+ {!> ../../../docs_src/request_files/tutorial003_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="11 18-20"
- {!> ../../../docs_src/request_files/tutorial003_an_py39.py!}
+ ```Python hl_lines="12 19-21"
+ {!> ../../../docs_src/request_files/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.9+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="11 18"
- {!> ../../../docs_src/request_files/tutorial003.py!}
+ ```Python hl_lines="9 16"
+ {!> ../../../docs_src/request_files/tutorial003_py39.py!}
```
-=== "Python 3.9 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="9 16"
- {!> ../../../docs_src/request_files/tutorial003_py39.py!}
+ ```Python hl_lines="11 18"
+ {!> ../../../docs_src/request_files/tutorial003.py!}
```
## Recap
## Import `File` and `Form`
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/request_forms_and_files/tutorial001_an.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/request_forms_and_files/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/request_forms_and_files/tutorial001_an_py39.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/request_forms_and_files/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Create file and form parameters the same way you would for `Body` or `Query`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9-11"
- {!> ../../../docs_src/request_forms_and_files/tutorial001_an.py!}
+ ```Python hl_lines="10-12"
+ {!> ../../../docs_src/request_forms_and_files/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="10-12"
- {!> ../../../docs_src/request_forms_and_files/tutorial001_an_py39.py!}
+ ```Python hl_lines="9-11"
+ {!> ../../../docs_src/request_forms_and_files/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Import `Form` from `fastapi`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/request_forms/tutorial001_an.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/request_forms/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/request_forms/tutorial001_an_py39.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/request_forms/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Create form parameters the same way you would for `Body` or `Query`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/request_forms/tutorial001_an.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/request_forms/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/request_forms/tutorial001_an_py39.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/request_forms/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
You can use **type annotations** the same way you would for input data in function **parameters**, you can use Pydantic models, lists, dictionaries, scalar values like integers, booleans, etc.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="18 23"
- {!> ../../../docs_src/response_model/tutorial001_01.py!}
+ ```Python hl_lines="16 21"
+ {!> ../../../docs_src/response_model/tutorial001_01_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="18 23"
{!> ../../../docs_src/response_model/tutorial001_01_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="16 21"
- {!> ../../../docs_src/response_model/tutorial001_01_py310.py!}
+ ```Python hl_lines="18 23"
+ {!> ../../../docs_src/response_model/tutorial001_01.py!}
```
FastAPI will use this return type to:
* `@app.delete()`
* etc.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="17 22 24-27"
- {!> ../../../docs_src/response_model/tutorial001.py!}
+ {!> ../../../docs_src/response_model/tutorial001_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="17 22 24-27"
{!> ../../../docs_src/response_model/tutorial001_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="17 22 24-27"
- {!> ../../../docs_src/response_model/tutorial001_py310.py!}
+ {!> ../../../docs_src/response_model/tutorial001.py!}
```
!!! note
Here we are declaring a `UserIn` model, it will contain a plaintext password:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9 11"
- {!> ../../../docs_src/response_model/tutorial002.py!}
+ ```Python hl_lines="7 9"
+ {!> ../../../docs_src/response_model/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7 9"
- {!> ../../../docs_src/response_model/tutorial002_py310.py!}
+ ```Python hl_lines="9 11"
+ {!> ../../../docs_src/response_model/tutorial002.py!}
```
!!! info
And we are using this model to declare our input and the same model to declare our output:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/response_model/tutorial002.py!}
+ ```Python hl_lines="16"
+ {!> ../../../docs_src/response_model/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="16"
- {!> ../../../docs_src/response_model/tutorial002_py310.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/response_model/tutorial002.py!}
```
Now, whenever a browser is creating a user with a password, the API will return the same password in the response.
We can instead create an input model with the plaintext password and an output model without it:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="9 11 16"
- {!> ../../../docs_src/response_model/tutorial003.py!}
+ {!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="9 11 16"
- {!> ../../../docs_src/response_model/tutorial003_py310.py!}
+ {!> ../../../docs_src/response_model/tutorial003.py!}
```
Here, even though our *path operation function* is returning the same input user that contains the password:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="24"
- {!> ../../../docs_src/response_model/tutorial003.py!}
+ {!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="24"
- {!> ../../../docs_src/response_model/tutorial003_py310.py!}
+ {!> ../../../docs_src/response_model/tutorial003.py!}
```
...we declared the `response_model` to be our model `UserOut`, that doesn't include the password:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python hl_lines="22"
- {!> ../../../docs_src/response_model/tutorial003.py!}
+ {!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python hl_lines="22"
- {!> ../../../docs_src/response_model/tutorial003_py310.py!}
+ {!> ../../../docs_src/response_model/tutorial003.py!}
```
So, **FastAPI** will take care of filtering out all the data that is not declared in the output model (using Pydantic).
And in those cases, we can use classes and inheritance to take advantage of function **type annotations** to get better support in the editor and tools, and still get the FastAPI **data filtering**.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9-13 15-16 20"
- {!> ../../../docs_src/response_model/tutorial003_01.py!}
+ ```Python hl_lines="7-10 13-14 18"
+ {!> ../../../docs_src/response_model/tutorial003_01_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7-10 13-14 18"
- {!> ../../../docs_src/response_model/tutorial003_01_py310.py!}
+ ```Python hl_lines="9-13 15-16 20"
+ {!> ../../../docs_src/response_model/tutorial003_01.py!}
```
With this, we get tooling support, from editors and mypy as this code is correct in terms of types, but we also get the data filtering from FastAPI.
The same would happen if you had something like a <abbr title='A union between multiple types means "any of these types".'>union</abbr> between different types where one or more of them are not valid Pydantic types, for example this would fail 💥:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/response_model/tutorial003_04.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/response_model/tutorial003_04_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/response_model/tutorial003_04_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/response_model/tutorial003_04.py!}
```
...this fails because the type annotation is not a Pydantic type and is not just a single `Response` class or subclass, it's a union (any of the two) between a `Response` and a `dict`.
In this case, you can disable the response model generation by setting `response_model=None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/response_model/tutorial003_05.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/response_model/tutorial003_05_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/response_model/tutorial003_05_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/response_model/tutorial003_05.py!}
```
This will make FastAPI skip the response model generation and that way you can have any return type annotations you need without it affecting your FastAPI application. 🤓
Your response model could have default values, like:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="11 13-14"
- {!> ../../../docs_src/response_model/tutorial004.py!}
+ ```Python hl_lines="9 11-12"
+ {!> ../../../docs_src/response_model/tutorial004_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="11 13-14"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="9 11-12"
- {!> ../../../docs_src/response_model/tutorial004_py310.py!}
+ ```Python hl_lines="11 13-14"
+ {!> ../../../docs_src/response_model/tutorial004.py!}
```
* `description: Union[str, None] = None` (or `str | None = None` in Python 3.10) has a default of `None`.
You can set the *path operation decorator* parameter `response_model_exclude_unset=True`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="24"
- {!> ../../../docs_src/response_model/tutorial004.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/response_model/tutorial004_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="22"
- {!> ../../../docs_src/response_model/tutorial004_py310.py!}
+ ```Python hl_lines="24"
+ {!> ../../../docs_src/response_model/tutorial004.py!}
```
and those default values won't be included in the response, only the values actually set.
This also applies to `response_model_by_alias` that works similarly.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="31 37"
- {!> ../../../docs_src/response_model/tutorial005.py!}
+ ```Python hl_lines="29 35"
+ {!> ../../../docs_src/response_model/tutorial005_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="29 35"
- {!> ../../../docs_src/response_model/tutorial005_py310.py!}
+ ```Python hl_lines="31 37"
+ {!> ../../../docs_src/response_model/tutorial005.py!}
```
!!! tip
If you forget to use a `set` and use a `list` or `tuple` instead, FastAPI will still convert it to a `set` and it will work correctly:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="31 37"
- {!> ../../../docs_src/response_model/tutorial006.py!}
+ ```Python hl_lines="29 35"
+ {!> ../../../docs_src/response_model/tutorial006_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="29 35"
- {!> ../../../docs_src/response_model/tutorial006_py310.py!}
+ ```Python hl_lines="31 37"
+ {!> ../../../docs_src/response_model/tutorial006.py!}
```
## Recap
You can declare an `example` for a Pydantic model using `Config` and `schema_extra`, as described in <a href="https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization" class="external-link" target="_blank">Pydantic's docs: Schema customization</a>:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="15-23"
- {!> ../../../docs_src/schema_extra_example/tutorial001.py!}
+ ```Python hl_lines="13-21"
+ {!> ../../../docs_src/schema_extra_example/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13-21"
- {!> ../../../docs_src/schema_extra_example/tutorial001_py310.py!}
+ ```Python hl_lines="15-23"
+ {!> ../../../docs_src/schema_extra_example/tutorial001.py!}
```
That extra info will be added as-is to the output **JSON Schema** for that model, and it will be used in the API docs.
You can use this to add `example` for each field:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4 10-13"
- {!> ../../../docs_src/schema_extra_example/tutorial002.py!}
+ ```Python hl_lines="2 8-11"
+ {!> ../../../docs_src/schema_extra_example/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="2 8-11"
- {!> ../../../docs_src/schema_extra_example/tutorial002_py310.py!}
+ ```Python hl_lines="4 10-13"
+ {!> ../../../docs_src/schema_extra_example/tutorial002.py!}
```
!!! warning
Here we pass an `example` of the data expected in `Body()`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="23-28"
- {!> ../../../docs_src/schema_extra_example/tutorial003_an.py!}
+ ```Python hl_lines="22-27"
+ {!> ../../../docs_src/schema_extra_example/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="22-27"
{!> ../../../docs_src/schema_extra_example/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="22-27"
- {!> ../../../docs_src/schema_extra_example/tutorial003_an_py310.py!}
+ ```Python hl_lines="23-28"
+ {!> ../../../docs_src/schema_extra_example/tutorial003_an.py!}
+ ```
+
+=== "Python 3.10+ non-Annotated"
+
+ ```Python hl_lines="18-23"
+ {!> ../../../docs_src/schema_extra_example/tutorial003_py310.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/schema_extra_example/tutorial003.py!}
```
-=== "Python 3.10 and above - non-Annotated"
-
- ```Python hl_lines="18-23"
- {!> ../../../docs_src/schema_extra_example/tutorial003_py310.py!}
- ```
-
### Example in the docs UI
With any of the methods above it would look like this in the `/docs`:
* `value`: This is the actual example shown, e.g. a `dict`.
* `externalValue`: alternative to `value`, a URL pointing to the example. Although this might not be supported by as many tools as `value`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="24-50"
- {!> ../../../docs_src/schema_extra_example/tutorial004_an.py!}
+ ```Python hl_lines="23-49"
+ {!> ../../../docs_src/schema_extra_example/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="23-49"
{!> ../../../docs_src/schema_extra_example/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="23-49"
- {!> ../../../docs_src/schema_extra_example/tutorial004_an_py310.py!}
+ ```Python hl_lines="24-50"
+ {!> ../../../docs_src/schema_extra_example/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
+
+ ```Python hl_lines="19-45"
+ {!> ../../../docs_src/schema_extra_example/tutorial004_py310.py!}
+ ```
+
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
{!> ../../../docs_src/schema_extra_example/tutorial004.py!}
```
-=== "Python 3.10 and above - non-Annotated"
-
- ```Python hl_lines="19-45"
- {!> ../../../docs_src/schema_extra_example/tutorial004_py310.py!}
- ```
-
### Examples in the docs UI
With `examples` added to `Body()` the `/docs` would look like:
Copy the example in a file `main.py`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python
- {!> ../../../docs_src/security/tutorial001_an.py!}
+ {!> ../../../docs_src/security/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/security/tutorial001_an_py39.py!}
+ {!> ../../../docs_src/security/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
When we create an instance of the `OAuth2PasswordBearer` class we pass in the `tokenUrl` parameter. This parameter contains the URL that the client (the frontend running in the user's browser) will use to send the `username` and `password` in order to get a token.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/security/tutorial001_an.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/security/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/security/tutorial001_an_py39.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/security/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
Now you can pass that `oauth2_scheme` in a dependency with `Depends`.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/security/tutorial001_an.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/security/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/security/tutorial001_an_py39.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/security/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
In the previous chapter the security system (which is based on the dependency injection system) was giving the *path operation function* a `token` as a `str`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="11"
- {!> ../../../docs_src/security/tutorial001_an.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/security/tutorial001_an_py39.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12"
- {!> ../../../docs_src/security/tutorial001_an_py39.py!}
+ ```Python hl_lines="11"
+ {!> ../../../docs_src/security/tutorial001_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
The same way we use Pydantic to declare bodies, we can use it anywhere else:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="5 13-17"
- {!> ../../../docs_src/security/tutorial002_an.py!}
+ ```Python hl_lines="5 12-16"
+ {!> ../../../docs_src/security/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="5 12-16"
{!> ../../../docs_src/security/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="5 12-16"
- {!> ../../../docs_src/security/tutorial002_an_py310.py!}
+ ```Python hl_lines="5 13-17"
+ {!> ../../../docs_src/security/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="5 12-16"
- {!> ../../../docs_src/security/tutorial002.py!}
+ ```Python hl_lines="3 10-14"
+ {!> ../../../docs_src/security/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3 10-14"
- {!> ../../../docs_src/security/tutorial002_py310.py!}
+ ```Python hl_lines="5 12-16"
+ {!> ../../../docs_src/security/tutorial002.py!}
```
## Create a `get_current_user` dependency
The same as we were doing before in the *path operation* directly, our new dependency `get_current_user` will receive a `token` as a `str` from the sub-dependency `oauth2_scheme`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="26"
- {!> ../../../docs_src/security/tutorial002_an.py!}
+ ```Python hl_lines="25"
+ {!> ../../../docs_src/security/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="25"
{!> ../../../docs_src/security/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="25"
- {!> ../../../docs_src/security/tutorial002_an_py310.py!}
+ ```Python hl_lines="26"
+ {!> ../../../docs_src/security/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="25"
- {!> ../../../docs_src/security/tutorial002.py!}
+ ```Python hl_lines="23"
+ {!> ../../../docs_src/security/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="23"
- {!> ../../../docs_src/security/tutorial002_py310.py!}
+ ```Python hl_lines="25"
+ {!> ../../../docs_src/security/tutorial002.py!}
```
## Get the user
`get_current_user` will use a (fake) utility function we created, that takes a token as a `str` and returns our Pydantic `User` model:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="20-23 27-28"
- {!> ../../../docs_src/security/tutorial002_an.py!}
+ ```Python hl_lines="19-22 26-27"
+ {!> ../../../docs_src/security/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="19-22 26-27"
{!> ../../../docs_src/security/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="19-22 26-27"
- {!> ../../../docs_src/security/tutorial002_an_py310.py!}
+ ```Python hl_lines="20-23 27-28"
+ {!> ../../../docs_src/security/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="19-22 26-27"
- {!> ../../../docs_src/security/tutorial002.py!}
+ ```Python hl_lines="17-20 24-25"
+ {!> ../../../docs_src/security/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="17-20 24-25"
- {!> ../../../docs_src/security/tutorial002_py310.py!}
+ ```Python hl_lines="19-22 26-27"
+ {!> ../../../docs_src/security/tutorial002.py!}
```
## Inject the current user
So now we can use the same `Depends` with our `get_current_user` in the *path operation*:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="32"
- {!> ../../../docs_src/security/tutorial002_an.py!}
+ ```Python hl_lines="31"
+ {!> ../../../docs_src/security/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="31"
{!> ../../../docs_src/security/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="31"
- {!> ../../../docs_src/security/tutorial002_an_py310.py!}
+ ```Python hl_lines="32"
+ {!> ../../../docs_src/security/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="31"
- {!> ../../../docs_src/security/tutorial002.py!}
+ ```Python hl_lines="29"
+ {!> ../../../docs_src/security/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="29"
- {!> ../../../docs_src/security/tutorial002_py310.py!}
+ ```Python hl_lines="31"
+ {!> ../../../docs_src/security/tutorial002.py!}
```
Notice that we declare the type of `current_user` as the Pydantic model `User`.
And all these thousands of *path operations* can be as small as 3 lines:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="31-33"
- {!> ../../../docs_src/security/tutorial002_an.py!}
+ ```Python hl_lines="30-32"
+ {!> ../../../docs_src/security/tutorial002_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="30-32"
{!> ../../../docs_src/security/tutorial002_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="30-32"
- {!> ../../../docs_src/security/tutorial002_an_py310.py!}
+ ```Python hl_lines="31-33"
+ {!> ../../../docs_src/security/tutorial002_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="30-32"
- {!> ../../../docs_src/security/tutorial002.py!}
+ ```Python hl_lines="28-30"
+ {!> ../../../docs_src/security/tutorial002_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="28-30"
- {!> ../../../docs_src/security/tutorial002_py310.py!}
+ ```Python hl_lines="30-32"
+ {!> ../../../docs_src/security/tutorial002.py!}
```
## Recap
And another one to authenticate and return a user.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="7 49 56-57 60-61 70-76"
- {!> ../../../docs_src/security/tutorial004_an.py!}
+ ```Python hl_lines="7 48 55-56 59-60 69-75"
+ {!> ../../../docs_src/security/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="7 48 55-56 59-60 69-75"
{!> ../../../docs_src/security/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7 48 55-56 59-60 69-75"
- {!> ../../../docs_src/security/tutorial004_an_py310.py!}
+ ```Python hl_lines="7 49 56-57 60-61 70-76"
+ {!> ../../../docs_src/security/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="7 48 55-56 59-60 69-75"
- {!> ../../../docs_src/security/tutorial004.py!}
+ ```Python hl_lines="6 47 54-55 58-59 68-74"
+ {!> ../../../docs_src/security/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="6 47 54-55 58-59 68-74"
- {!> ../../../docs_src/security/tutorial004_py310.py!}
+ ```Python hl_lines="7 48 55-56 59-60 69-75"
+ {!> ../../../docs_src/security/tutorial004.py!}
```
!!! note
Create a utility function to generate a new access token.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="6 13-15 29-31 79-87"
- {!> ../../../docs_src/security/tutorial004_an.py!}
+ ```Python hl_lines="6 12-14 28-30 78-86"
+ {!> ../../../docs_src/security/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="6 12-14 28-30 78-86"
{!> ../../../docs_src/security/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6 12-14 28-30 78-86"
- {!> ../../../docs_src/security/tutorial004_an_py310.py!}
+ ```Python hl_lines="6 13-15 29-31 79-87"
+ {!> ../../../docs_src/security/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="6 12-14 28-30 78-86"
- {!> ../../../docs_src/security/tutorial004.py!}
+ ```Python hl_lines="5 11-13 27-29 77-85"
+ {!> ../../../docs_src/security/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="5 11-13 27-29 77-85"
- {!> ../../../docs_src/security/tutorial004_py310.py!}
+ ```Python hl_lines="6 12-14 28-30 78-86"
+ {!> ../../../docs_src/security/tutorial004.py!}
```
## Update the dependencies
If the token is invalid, return an HTTP error right away.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="90-107"
- {!> ../../../docs_src/security/tutorial004_an.py!}
+ ```Python hl_lines="89-106"
+ {!> ../../../docs_src/security/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="89-106"
{!> ../../../docs_src/security/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="89-106"
- {!> ../../../docs_src/security/tutorial004_an_py310.py!}
+ ```Python hl_lines="90-107"
+ {!> ../../../docs_src/security/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="89-106"
- {!> ../../../docs_src/security/tutorial004.py!}
+ ```Python hl_lines="88-105"
+ {!> ../../../docs_src/security/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="88-105"
- {!> ../../../docs_src/security/tutorial004_py310.py!}
+ ```Python hl_lines="89-106"
+ {!> ../../../docs_src/security/tutorial004.py!}
```
## Update the `/token` *path operation*
Create a `timedelta` with the expiration time of the token.
-Create a real JWT access token and return it.
+Create a real JWT access token and return it
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="118-133"
- {!> ../../../docs_src/security/tutorial004_an.py!}
+ ```Python hl_lines="117-132"
+ {!> ../../../docs_src/security/tutorial004_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="117-132"
{!> ../../../docs_src/security/tutorial004_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="117-132"
- {!> ../../../docs_src/security/tutorial004_an_py310.py!}
+ ```Python hl_lines="118-133"
+ {!> ../../../docs_src/security/tutorial004_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="115-128"
- {!> ../../../docs_src/security/tutorial004.py!}
+ ```Python hl_lines="114-127"
+ {!> ../../../docs_src/security/tutorial004_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="114-127"
- {!> ../../../docs_src/security/tutorial004_py310.py!}
+ ```Python hl_lines="115-128"
+ {!> ../../../docs_src/security/tutorial004.py!}
```
### Technical details about the JWT "subject" `sub`
First, import `OAuth2PasswordRequestForm`, and use it as a dependency with `Depends` in the *path operation* for `/token`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="4 79"
- {!> ../../../docs_src/security/tutorial003_an.py!}
+ ```Python hl_lines="4 78"
+ {!> ../../../docs_src/security/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="4 78"
{!> ../../../docs_src/security/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 78"
- {!> ../../../docs_src/security/tutorial003_an_py310.py!}
+ ```Python hl_lines="4 79"
+ {!> ../../../docs_src/security/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="4 76"
- {!> ../../../docs_src/security/tutorial003.py!}
+ ```Python hl_lines="2 74"
+ {!> ../../../docs_src/security/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="2 74"
- {!> ../../../docs_src/security/tutorial003_py310.py!}
+ ```Python hl_lines="4 76"
+ {!> ../../../docs_src/security/tutorial003.py!}
```
`OAuth2PasswordRequestForm` is a class dependency that declares a form body with:
For the error, we use the exception `HTTPException`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="3 80-82"
- {!> ../../../docs_src/security/tutorial003_an.py!}
+ ```Python hl_lines="3 79-81"
+ {!> ../../../docs_src/security/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3 79-81"
{!> ../../../docs_src/security/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="3 79-81"
- {!> ../../../docs_src/security/tutorial003_an_py310.py!}
+ ```Python hl_lines="3 80-82"
+ {!> ../../../docs_src/security/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="3 77-79"
- {!> ../../../docs_src/security/tutorial003.py!}
+ ```Python hl_lines="1 75-77"
+ {!> ../../../docs_src/security/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="1 75-77"
- {!> ../../../docs_src/security/tutorial003_py310.py!}
+ ```Python hl_lines="3 77-79"
+ {!> ../../../docs_src/security/tutorial003.py!}
```
### Check the password
So, the thief won't be able to try to use those same passwords in another system (as many users use the same password everywhere, this would be dangerous).
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="83-86"
- {!> ../../../docs_src/security/tutorial003_an.py!}
+ ```Python hl_lines="82-85"
+ {!> ../../../docs_src/security/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="82-85"
{!> ../../../docs_src/security/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="82-85"
- {!> ../../../docs_src/security/tutorial003_an_py310.py!}
+ ```Python hl_lines="83-86"
+ {!> ../../../docs_src/security/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="80-83"
- {!> ../../../docs_src/security/tutorial003.py!}
+ ```Python hl_lines="78-81"
+ {!> ../../../docs_src/security/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="78-81"
- {!> ../../../docs_src/security/tutorial003_py310.py!}
+ ```Python hl_lines="80-83"
+ {!> ../../../docs_src/security/tutorial003.py!}
```
#### About `**user_dict`
But for now, let's focus on the specific details we need.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="88"
- {!> ../../../docs_src/security/tutorial003_an.py!}
+ ```Python hl_lines="87"
+ {!> ../../../docs_src/security/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="87"
{!> ../../../docs_src/security/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="87"
- {!> ../../../docs_src/security/tutorial003_an_py310.py!}
+ ```Python hl_lines="88"
+ {!> ../../../docs_src/security/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="85"
- {!> ../../../docs_src/security/tutorial003.py!}
+ ```Python hl_lines="83"
+ {!> ../../../docs_src/security/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="83"
- {!> ../../../docs_src/security/tutorial003_py310.py!}
+ ```Python hl_lines="85"
+ {!> ../../../docs_src/security/tutorial003.py!}
```
!!! tip
So, in our endpoint, we will only get a user if the user exists, was correctly authenticated, and is active:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="59-67 70-75 95"
- {!> ../../../docs_src/security/tutorial003_an.py!}
+ ```Python hl_lines="58-66 69-74 94"
+ {!> ../../../docs_src/security/tutorial003_an_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="58-66 69-74 94"
{!> ../../../docs_src/security/tutorial003_an_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="58-66 69-74 94"
- {!> ../../../docs_src/security/tutorial003_an_py310.py!}
+ ```Python hl_lines="59-67 70-75 95"
+ {!> ../../../docs_src/security/tutorial003_an.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="58-66 69-72 90"
- {!> ../../../docs_src/security/tutorial003.py!}
+ ```Python hl_lines="56-64 67-70 88"
+ {!> ../../../docs_src/security/tutorial003_py310.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
- ```Python hl_lines="56-64 67-70 88"
- {!> ../../../docs_src/security/tutorial003_py310.py!}
+ ```Python hl_lines="58-66 69-72 90"
+ {!> ../../../docs_src/security/tutorial003.py!}
```
!!! info
But for security, the `password` won't be in other Pydantic *models*, for example, it won't be sent from the API when reading a user.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="3 6-8 11-12 23-24 27-28"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="1 4-6 9-10 21-22 25-26"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="3 6-8 11-12 23-24 27-28"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1 4-6 9-10 21-22 25-26"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="3 6-8 11-12 23-24 27-28"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
#### SQLAlchemy style and Pydantic style
Not only the IDs of those items, but all the data that we defined in the Pydantic *model* for reading items: `Item`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="15-17 31-34"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="13-15 29-32"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="15-17 31-34"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13-15 29-32"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="15-17 31-34"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
!!! tip
In the `Config` class, set the attribute `orm_mode = True`.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="15 19-20 31 36-37"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="13 17-18 29 34-35"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="15 19-20 31 36-37"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13 17-18 29 34-35"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="15 19-20 31 36-37"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
!!! tip
In a very simplistic way create the database tables:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
#### Alembic Note
Our dependency will create a new SQLAlchemy `SessionLocal` that will be used in a single request, and then close it once the request is finished.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="15-20"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="13-18"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="13-18"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="15-20"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
!!! info
This will then give us better editor support inside the *path operation function*, because the editor will know that the `db` parameter is of type `Session`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="24 32 38 47 53"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="22 30 36 45 51"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="22 30 36 45 51"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="24 32 38 47 53"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
!!! info "Technical Details"
Now, finally, here's the standard **FastAPI** *path operations* code.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="23-28 31-34 37-42 45-49 52-55"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="21-26 29-32 35-40 43-47 50-53"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="21-26 29-32 35-40 43-47 50-53"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="23-28 31-34 37-42 45-49 52-55"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
We are creating the database session before each request in the dependency with `yield`, and then closing it afterwards.
* `sql_app/schemas.py`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
* `sql_app/crud.py`:
* `sql_app/main.py`:
-=== "Python 3.6 and above"
+=== "Python 3.9+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
## Check it
The middleware we'll add (just a function) will create a new SQLAlchemy `SessionLocal` for each request, add it to the request and then close it once the request is finished.
-=== "Python 3.6 and above"
+=== "Python 3.9+"
- ```Python hl_lines="14-22"
- {!> ../../../docs_src/sql_databases/sql_app/alt_main.py!}
+ ```Python hl_lines="12-20"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/alt_main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.6+"
- ```Python hl_lines="12-20"
- {!> ../../../docs_src/sql_databases/sql_app_py39/alt_main.py!}
+ ```Python hl_lines="14-22"
+ {!> ../../../docs_src/sql_databases/sql_app/alt_main.py!}
```
!!! info
Both *path operations* require an `X-Token` header.
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- {!> ../../../docs_src/app_testing/app_b_an/main.py!}
+ {!> ../../../docs_src/app_testing/app_b_an_py310/main.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python
{!> ../../../docs_src/app_testing/app_b_an_py39/main.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/app_testing/app_b_an_py310/main.py!}
+ {!> ../../../docs_src/app_testing/app_b_an/main.py!}
```
-=== "Python 3.6 and above - non-Annotated"
+=== "Python 3.10+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
```Python
- {!> ../../../docs_src/app_testing/app_b/main.py!}
+ {!> ../../../docs_src/app_testing/app_b_py310/main.py!}
```
-=== "Python 3.10 and above - non-Annotated"
+=== "Python 3.6+ non-Annotated"
!!! tip
Try to use the main, `Annotated` version better.
```Python
- {!> ../../../docs_src/app_testing/app_b_py310/main.py!}
+ {!> ../../../docs_src/app_testing/app_b/main.py!}
```
### Extended testing file
これらの *path operation* には `X-Token` ヘッダーが必要です。
-=== "Python 3.6 and above"
+=== "Python 3.10+"
```Python
- {!> ../../../docs_src/app_testing/app_b/main.py!}
+ {!> ../../../docs_src/app_testing/app_b_py310/main.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/app_testing/app_b_py310/main.py!}
+ {!> ../../../docs_src/app_testing/app_b/main.py!}
```
### 拡張版テストファイル
E você também pode declarar parâmetros de corpo como opcionais, definindo o valor padrão com `None`:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="19-21"
- {!> ../../../docs_src/body_multiple_params/tutorial001.py!}
+ ```Python hl_lines="17-19"
+ {!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="17-19"
- {!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!}
+ ```Python hl_lines="19-21"
+ {!> ../../../docs_src/body_multiple_params/tutorial001.py!}
```
!!! nota
Mas você pode também declarar múltiplos parâmetros de corpo, por exemplo, `item` e `user`:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="22"
- {!> ../../../docs_src/body_multiple_params/tutorial002.py!}
+ ```Python hl_lines="20"
+ {!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="20"
- {!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!}
+ ```Python hl_lines="22"
+ {!> ../../../docs_src/body_multiple_params/tutorial002.py!}
```
Neste caso, o **FastAPI** perceberá que existe mais de um parâmetro de corpo na função (dois parâmetros que são modelos Pydantic).
Mas você pode instruir o **FastAPI** para tratá-lo como outra chave do corpo usando `Body`:
-=== "Python 3.6 e superiores"
+=== "Python 3.6+"
```Python hl_lines="22"
{!> ../../../docs_src/body_multiple_params/tutorial003.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.10+"
```Python hl_lines="20"
{!> ../../../docs_src/body_multiple_params/tutorial003_py310.py!}
Por exemplo:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="27"
- {!> ../../../docs_src/body_multiple_params/tutorial004.py!}
+ ```Python hl_lines="26"
+ {!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="26"
- {!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!}
+ ```Python hl_lines="27"
+ {!> ../../../docs_src/body_multiple_params/tutorial004.py!}
```
!!! info "Informação"
como em:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/body_multiple_params/tutorial005.py!}
+ ```Python hl_lines="15"
+ {!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="15"
- {!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/body_multiple_params/tutorial005.py!}
```
Neste caso o **FastAPI** esperará um corpo como:
A função recebe um objeto, como um modelo Pydantic e retorna uma versão compatível com JSON:
-=== "Python 3.6 e acima"
+=== "Python 3.10+"
- ```Python hl_lines="5 22"
- {!> ../../../docs_src/encoder/tutorial001.py!}
+ ```Python hl_lines="4 21"
+ {!> ../../../docs_src/encoder/tutorial001_py310.py!}
```
-=== "Python 3.10 e acima"
+=== "Python 3.6+"
- ```Python hl_lines="4 21"
- {!> ../../../docs_src/encoder/tutorial001_py310.py!}
+ ```Python hl_lines="5 22"
+ {!> ../../../docs_src/encoder/tutorial001.py!}
```
Neste exemplo, ele converteria o modelo Pydantic em um `dict`, e o `datetime` em um `str`.
Primeiro importe `Header`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/header_params/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/header_params/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/header_params/tutorial001.py!}
```
## Declare parâmetros de `Header`
O primeiro valor é o valor padrão, você pode passar todas as validações adicionais ou parâmetros de anotação:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/header_params/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial001.py!}
```
!!! note "Detalhes Técnicos"
Se por algum motivo você precisar desabilitar a conversão automática de sublinhados para hífens, defina o parâmetro `convert_underscores` de `Header` para `False`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/header_params/tutorial002.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/header_params/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/header_params/tutorial002_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/header_params/tutorial002.py!}
```
!!! warning "Aviso"
Por exemplo, para declarar um cabeçalho de `X-Token` que pode aparecer mais de uma vez, você pode escrever:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/header_params/tutorial003.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/header_params/tutorial003_py310.py!}
```
-=== "Python 3.9 and above"
+=== "Python 3.9+"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial003_py39.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/header_params/tutorial003_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/header_params/tutorial003.py!}
```
Se você se comunicar com essa *operação de caminho* enviando dois cabeçalhos HTTP como:
Primeiro, importe `Path` de `fastapi`:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
## Declare metadados
Por exemplo para declarar um valor de metadado `title` para o parâmetro de rota `item_id` você pode digitar:
-=== "Python 3.6 e superiores"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
-=== "Python 3.10 e superiores"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
!!! note "Nota"
Da mesma forma, você pode declarar parâmetros de consulta opcionais, definindo o valor padrão para `None`:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params/tutorial002.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params/tutorial002_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params/tutorial002_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params/tutorial002.py!}
```
Nesse caso, o parâmetro da função `q` será opcional, e `None` será o padrão.
Você também pode declarar tipos `bool`, e eles serão convertidos:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/query_params/tutorial003.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/query_params/tutorial003_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/query_params/tutorial003_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/query_params/tutorial003.py!}
```
Nesse caso, se você for para:
Eles serão detectados pelo nome:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="8 10"
- {!> ../../../docs_src/query_params/tutorial004.py!}
+ ```Python hl_lines="6 8"
+ {!> ../../../docs_src/query_params/tutorial004_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="6 8"
- {!> ../../../docs_src/query_params/tutorial004_py310.py!}
+ ```Python hl_lines="8 10"
+ {!> ../../../docs_src/query_params/tutorial004.py!}
```
## Parâmetros de consulta obrigatórios
E claro, você pode definir alguns parâmetros como obrigatórios, alguns possuindo um valor padrão, e outros sendo totalmente opcionais:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="10"
- {!> ../../../docs_src/query_params/tutorial006.py!}
+ ```Python hl_lines="8"
+ {!> ../../../docs_src/query_params/tutorial006_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="8"
- {!> ../../../docs_src/query_params/tutorial006_py310.py!}
+ ```Python hl_lines="10"
+ {!> ../../../docs_src/query_params/tutorial006.py!}
```
+
Nesse caso, existem 3 parâmetros de consulta:
* `needy`, um `str` obrigatório.
**FastAPI** знает, что нужно сделать в каждом случае и как переиспользовать тот же объект `BackgroundTasks`, так чтобы все фоновые задачи собрались и запустились вместе в фоне:
-=== "Python 3.6 и выше"
+=== "Python 3.10+"
- ```Python hl_lines="13 15 22 25"
- {!> ../../../docs_src/background_tasks/tutorial002.py!}
+ ```Python hl_lines="11 13 20 23"
+ {!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
```
-=== "Python 3.10 и выше"
+=== "Python 3.6+"
- ```Python hl_lines="11 13 20 23"
- {!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
+ ```Python hl_lines="13 15 22 25"
+ {!> ../../../docs_src/background_tasks/tutorial002.py!}
```
В этом примере сообщения будут записаны в `log.txt` *после* того, как ответ сервера был отправлен.
Сначала вы должны импортировать его:
-=== "Python 3.6 и выше"
+=== "Python 3.10+"
- ```Python hl_lines="4"
- {!> ../../../docs_src/body_fields/tutorial001.py!}
+ ```Python hl_lines="2"
+ {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
-=== "Python 3.10 и выше"
+=== "Python 3.6+"
- ```Python hl_lines="2"
- {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
+ ```Python hl_lines="4"
+ {!> ../../../docs_src/body_fields/tutorial001.py!}
```
!!! warning "Внимание"
Вы можете использовать функцию `Field` с атрибутами модели:
-=== "Python 3.6 и выше"
+=== "Python 3.10+"
- ```Python hl_lines="11-14"
- {!> ../../../docs_src/body_fields/tutorial001.py!}
+ ```Python hl_lines="9-12"
+ {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
-=== "Python 3.10 и выше"
+=== "Python 3.6+"
- ```Python hl_lines="9-12"
- {!> ../../../docs_src/body_fields/tutorial001_py310.py!}
+ ```Python hl_lines="11-14"
+ {!> ../../../docs_src/body_fields/tutorial001.py!}
```
Функция `Field` работает так же, как `Query`, `Path` и `Body`, у ее такие же параметры и т.д.
Сначала импортируйте `Cookie`:
-=== "Python 3.6 и выше"
+=== "Python 3.10+"
- ```Python hl_lines="3"
- {!> ../../../docs_src/cookie_params/tutorial001.py!}
+ ```Python hl_lines="1"
+ {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
-=== "Python 3.10 и выше"
+=== "Python 3.6+"
- ```Python hl_lines="1"
- {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
+ ```Python hl_lines="3"
+ {!> ../../../docs_src/cookie_params/tutorial001.py!}
```
## Объявление параметров `Cookie`
Первое значение - это значение по умолчанию, вы можете передать все дополнительные параметры проверки или аннотации:
-=== "Python 3.6 и выше"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/cookie_params/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
-=== "Python 3.10 и выше"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/cookie_params/tutorial001.py!}
```
!!! note "Технические детали"
在前面的例子中, 我们从依赖项 ("可依赖对象") 中返回了一个 `dict`:
-=== "Python 3.6 以及 以上"
+=== "Python 3.10+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
-=== "Python 3.10 以及以上"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/dependencies/tutorial001_py310.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
```
但是后面我们在路径操作函数的参数 `commons` 中得到了一个 `dict`。
所以,我们可以将上面的依赖项 "可依赖对象" `common_parameters` 更改为类 `CommonQueryParams`:
-=== "Python 3.6 以及 以上"
-
- ```Python hl_lines="11-15"
- {!> ../../../docs_src/dependencies/tutorial002.py!}
- ```
-
-=== "Python 3.10 以及 以上"
+=== "Python 3.10+"
```Python hl_lines="9-13"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-注意用于创建类实例的 `__init__` 方法:
-
-=== "Python 3.6 以及 以上"
+=== "Python 3.6+"
- ```Python hl_lines="12"
+ ```Python hl_lines="11-15"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```
-=== "Python 3.10 以及 以上"
+注意用于创建类实例的 `__init__` 方法:
+
+=== "Python 3.10+"
```Python hl_lines="10"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-...它与我们以前的 `common_parameters` 具有相同的参数:
-
-=== "Python 3.6 以及 以上"
+=== "Python 3.6+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```Python hl_lines="12"
+ {!> ../../../docs_src/dependencies/tutorial002.py!}
```
-=== "Python 3.10 以及 以上"
+...它与我们以前的 `common_parameters` 具有相同的参数:
+
+=== "Python 3.10+"
```Python hl_lines="6"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
+=== "Python 3.6+"
+
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/dependencies/tutorial001.py!}
+ ```
+
这些参数就是 **FastAPI** 用来 "处理" 依赖项的。
在两个例子下,都有:
现在,您可以使用这个类来声明你的依赖项了。
-=== "Python 3.6 以及 以上"
+=== "Python 3.10+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial002.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
-=== "Python 3.10 以及 以上"
+=== "Python 3.6+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial002_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial002.py!}
```
**FastAPI** 调用 `CommonQueryParams` 类。这将创建该类的一个 "实例",该实例将作为参数 `commons` 被传递给你的函数。
..就像:
-=== "Python 3.6 以及 以上"
+=== "Python 3.10+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial003.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial003_py310.py!}
```
-=== "Python 3.10 以及 以上"
+=== "Python 3.6+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial003_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial003.py!}
```
但是声明类型是被鼓励的,因为那样你的编辑器就会知道将传递什么作为参数 `commons` ,然后它可以帮助你完成代码,类型检查,等等:
同样的例子看起来像这样:
-=== "Python 3.6 以及 以上"
+=== "Python 3.10+"
- ```Python hl_lines="19"
- {!> ../../../docs_src/dependencies/tutorial004.py!}
+ ```Python hl_lines="17"
+ {!> ../../../docs_src/dependencies/tutorial004_py310.py!}
```
-=== "Python 3.10 以及 以上"
+=== "Python 3.6+"
- ```Python hl_lines="17"
- {!> ../../../docs_src/dependencies/tutorial004_py310.py!}
+ ```Python hl_lines="19"
+ {!> ../../../docs_src/dependencies/tutorial004.py!}
```
... **FastAPI** 会知道怎么处理。
它接收一个对象,比如Pydantic模型,并会返回一个JSON兼容的版本:
-=== "Python 3.6 and above"
+=== "Python 3.10+"
- ```Python hl_lines="5 22"
- {!> ../../../docs_src/encoder/tutorial001.py!}
+ ```Python hl_lines="4 21"
+ {!> ../../../docs_src/encoder/tutorial001_py310.py!}
```
-=== "Python 3.10 and above"
+=== "Python 3.6+"
- ```Python hl_lines="4 21"
- {!> ../../../docs_src/encoder/tutorial001_py310.py!}
+ ```Python hl_lines="5 22"
+ {!> ../../../docs_src/encoder/tutorial001.py!}
```
在这个例子中,它将Pydantic模型转换为`dict`,并将`datetime`转换为`str`。
您可以通过使用标准类型注解并将 None 作为默认值的方式将一个文件参数设为可选:
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="9 17"
- {!> ../../../docs_src/request_files/tutorial001_02.py!}
+ ```Python hl_lines="7 14"
+ {!> ../../../docs_src/request_files/tutorial001_02_py310.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="7 14"
- {!> ../../../docs_src/request_files/tutorial001_02_py310.py!}
+ ```Python hl_lines="9 17"
+ {!> ../../../docs_src/request_files/tutorial001_02.py!}
```
## 带有额外元数据的 `UploadFile`
上传多个文件时,要声明含 `bytes` 或 `UploadFile` 的列表(`List`):
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="10 15"
- {!> ../../../docs_src/request_files/tutorial002.py!}
+ ```Python hl_lines="8 13"
+ {!> ../../../docs_src/request_files/tutorial002_py39.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="8 13"
- {!> ../../../docs_src/request_files/tutorial002_py39.py!}
+ ```Python hl_lines="10 15"
+ {!> ../../../docs_src/request_files/tutorial002.py!}
```
接收的也是含 `bytes` 或 `UploadFile` 的列表(`list`)。
和之前的方式一样, 您可以为 `File()` 设置额外参数, 即使是 `UploadFile`:
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="18"
- {!> ../../../docs_src/request_files/tutorial003.py!}
+ ```Python hl_lines="16"
+ {!> ../../../docs_src/request_files/tutorial003_py39.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="16"
- {!> ../../../docs_src/request_files/tutorial003_py39.py!}
+ ```Python hl_lines="18"
+ {!> ../../../docs_src/request_files/tutorial003.py!}
```
## 小结
但是为了安全起见,`password`不会出现在其他同类 Pydantic*模型*中,例如用户请求时不应该从 API 返回响应中包含它。
-=== "Python 3.6 及以上版本"
+=== "Python 3.10+"
- ```Python hl_lines="3 6-8 11-12 23-24 27-28"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="1 4-6 9-10 21-22 25-26"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.9+"
```Python hl_lines="3 6-8 11-12 23-24 27-28"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="1 4-6 9-10 21-22 25-26"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="3 6-8 11-12 23-24 27-28"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
#### SQLAlchemy 风格和 Pydantic 风格
不仅是这些项目的 ID,还有我们在 Pydantic*模型*中定义的用于读取项目的所有数据:`Item`.
-=== "Python 3.6 及以上版本"
+=== "Python 3.10+"
- ```Python hl_lines="15-17 31-34"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="13-15 29-32"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.9+"
```Python hl_lines="15-17 31-34"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="13-15 29-32"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="15-17 31-34"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
!!! tip
在`Config`类中,设置属性`orm_mode = True`。
-=== "Python 3.6 及以上版本"
+=== "Python 3.10+"
- ```Python hl_lines="15 19-20 31 36-37"
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ ```Python hl_lines="13 17-18 29 34-35"
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.9+"
```Python hl_lines="15 19-20 31 36-37"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="13 17-18 29 34-35"
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ ```Python hl_lines="15 19-20 31 36-37"
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
!!! tip
以非常简单的方式创建数据库表:
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="9"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="7"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="7"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="9"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
#### Alembic 注意
我们的依赖项将创建一个新的 SQLAlchemy `SessionLocal`,它将在单个请求中使用,然后在请求完成后关闭它。
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="15-20"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="13-18"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="13-18"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="15-20"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
!!! info
*这将为我们在路径操作函数*中提供更好的编辑器支持,因为编辑器将知道`db`参数的类型`Session`:
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="24 32 38 47 53"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="22 30 36 45 51"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="22 30 36 45 51"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="24 32 38 47 53"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
!!! info "技术细节"
现在,到了最后,编写标准的**FastAPI** *路径操作*代码。
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="23-28 31-34 37-42 45-49 52-55"
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ ```Python hl_lines="21-26 29-32 35-40 43-47 50-53"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="21-26 29-32 35-40 43-47 50-53"
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ ```Python hl_lines="23-28 31-34 37-42 45-49 52-55"
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
我们在依赖项中的每个请求之前利用`yield`创建数据库会话,然后关闭它。
* `sql_app/schemas.py`:
-=== "Python 3.6 及以上版本"
+=== "Python 3.10+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
+ {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.9+"
```Python
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
-=== "Python 3.10 及以上版本"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
+ {!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
* `sql_app/crud.py`:
* `sql_app/main.py`:
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app/main.py!}
+ {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
```Python
- {!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
+ {!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
## 执行项目
我们将添加中间件(只是一个函数)将为每个请求创建一个新的 SQLAlchemy`SessionLocal`,将其添加到请求中,然后在请求完成后关闭它。
-=== "Python 3.6 及以上版本"
+=== "Python 3.9+"
- ```Python hl_lines="14-22"
- {!> ../../../docs_src/sql_databases/sql_app/alt_main.py!}
+ ```Python hl_lines="12-20"
+ {!> ../../../docs_src/sql_databases/sql_app_py39/alt_main.py!}
```
-=== "Python 3.9 及以上版本"
+=== "Python 3.6+"
- ```Python hl_lines="12-20"
- {!> ../../../docs_src/sql_databases/sql_app_py39/alt_main.py!}
+ ```Python hl_lines="14-22"
+ {!> ../../../docs_src/sql_databases/sql_app/alt_main.py!}
```
!!! info