mirror of https://github.com/tiangolo/fastapi.git
📝 Tweak intro docs about `Annotated` and `Query()` params (#11664)
This commit is contained in:
parent
995bd43619
commit
ab8974ef04
|
|
@ -476,7 +476,7 @@ You will see a lot more of all this in practice in the [Tutorial - User Guide](t
|
||||||
|
|
||||||
## Type Hints with Metadata Annotations
|
## Type Hints with Metadata Annotations
|
||||||
|
|
||||||
Python also has a feature that allows putting **additional metadata** in these type hints using `Annotated`.
|
Python also has a feature that allows putting **additional <abbr title="Data about the data, in this case, information about the type, e.g. a description.">metadata</abbr>** in these type hints using `Annotated`.
|
||||||
|
|
||||||
=== "Python 3.9+"
|
=== "Python 3.9+"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ Now let's jump to the fun stuff. 🎉
|
||||||
|
|
||||||
## Add `Query` to `Annotated` in the `q` parameter
|
## Add `Query` to `Annotated` in the `q` parameter
|
||||||
|
|
||||||
Now that we have this `Annotated` where we can put more metadata, add `Query` to it, and set the parameter `max_length` to 50:
|
Now that we have this `Annotated` where we can put more information (in this case some additional validation), add `Query` inside of `Annotated`, and set the parameter `max_length` to `50`:
|
||||||
|
|
||||||
=== "Python 3.10+"
|
=== "Python 3.10+"
|
||||||
|
|
||||||
|
|
@ -115,7 +115,11 @@ Now that we have this `Annotated` where we can put more metadata, add `Query` to
|
||||||
|
|
||||||
Notice that the default value is still `None`, so the parameter is still optional.
|
Notice that the default value is still `None`, so the parameter is still optional.
|
||||||
|
|
||||||
But now, having `Query(max_length=50)` inside of `Annotated`, we are telling FastAPI that we want it to extract this value from the query parameters (this would have been the default anyway 🤷) and that we want to have **additional validation** for this value (that's why we do this, to get the additional validation). 😎
|
But now, having `Query(max_length=50)` inside of `Annotated`, we are telling FastAPI that we want it to have **additional validation** for this value, we want it to have maximum 50 characters. 😎
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
|
||||||
|
Here we are using `Query()` because this is a **query parameter**. Later we will see others like `Path()`, `Body()`, `Header()`, and `Cookie()`, that also accept the same arguments as `Query()`.
|
||||||
|
|
||||||
FastAPI will now:
|
FastAPI will now:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue