mirror of https://github.com/tiangolo/fastapi.git
Updated or more precise links
This commit is contained in:
parent
093a75d885
commit
6d0c283cef
|
|
@ -36,7 +36,7 @@ This part is pretty normal, most of the code is probably already familiar to you
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
The `callback_url` query parameter uses a Pydantic <a href="https://docs.pydantic.dev/latest/concepts/types/#urls" class="external-link" target="_blank">URL</a> type.
|
The `callback_url` query parameter uses a Pydantic <a href="https://docs.pydantic.dev/latest/api/networks/" class="external-link" target="_blank">Url</a> type.
|
||||||
|
|
||||||
The only new thing is the `callbacks=invoices_callback_router.routes` as an argument to the *path operation decorator*. We'll see what that is next.
|
The only new thing is the `callbacks=invoices_callback_router.routes` as an argument to the *path operation decorator*. We'll see what that is next.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,7 @@ That means that any value read in Python from an environment variable will be a
|
||||||
|
|
||||||
## Pydantic `Settings`
|
## Pydantic `Settings`
|
||||||
|
|
||||||
Fortunately, Pydantic provides a great utility to handle these settings coming from environment variables with <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" class="external-link" target="_blank">Pydantic: Settings management</a>.
|
Fortunately, Pydantic provides a great utility to handle these settings coming from environment variables with <a href="https://docs.pydantic.dev/latest/concepts/pydantic_settings/" class="external-link" target="_blank">Pydantic: Settings management</a>.
|
||||||
|
|
||||||
### Install `pydantic-settings`
|
### Install `pydantic-settings`
|
||||||
|
|
||||||
|
|
@ -351,7 +351,7 @@ And then update your `config.py` with:
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
The `model_config` attribute is used just for Pydantic configuration. You can read more at <a href="https://docs.pydantic.dev/latest/usage/model_config/" class="external-link" target="_blank">Pydantic Model Config</a>.
|
The `model_config` attribute is used just for Pydantic configuration. You can read more at <a href="https://docs.pydantic.dev/latest/concepts/config/" class="external-link" target="_blank">Pydantic: Concepts: Configuration</a>.
|
||||||
|
|
||||||
=== "Pydantic v1"
|
=== "Pydantic v1"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ I love to hear about how **FastAPI** is being used, what you have liked in it, i
|
||||||
## Vote for FastAPI
|
## Vote for FastAPI
|
||||||
|
|
||||||
* <a href="https://www.slant.co/options/34241/~fastapi-review" class="external-link" target="_blank">Vote for **FastAPI** in Slant</a>.
|
* <a href="https://www.slant.co/options/34241/~fastapi-review" class="external-link" target="_blank">Vote for **FastAPI** in Slant</a>.
|
||||||
* <a href="https://alternativeto.net/software/fastapi/" class="external-link" target="_blank">Vote for **FastAPI** in AlternativeTo</a>.
|
* <a href="https://alternativeto.net/software/fastapi/about/" class="external-link" target="_blank">Vote for **FastAPI** in AlternativeTo</a>.
|
||||||
* <a href="https://stackshare.io/pypi-fastapi" class="external-link" target="_blank">Say you use **FastAPI** on StackShare</a>.
|
* <a href="https://stackshare.io/pypi-fastapi" class="external-link" target="_blank">Say you use **FastAPI** on StackShare</a>.
|
||||||
|
|
||||||
## Help others with questions in GitHub
|
## Help others with questions in GitHub
|
||||||
|
|
|
||||||
|
|
@ -472,7 +472,7 @@ An example from the official Pydantic docs:
|
||||||
You will see a lot more of all this in practice in the [Tutorial - User Guide](tutorial/index.md){.internal-link target=_blank}.
|
You will see a lot more of all this in practice in the [Tutorial - User Guide](tutorial/index.md){.internal-link target=_blank}.
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
Pydantic has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://docs.pydantic.dev/latest/concepts/models/#required-optional-fields" class="external-link" target="_blank">Required Optional fields</a>.
|
Pydantic has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://docs.pydantic.dev/2.3/usage/models/#required-fields" class="external-link" target="_blank">Required fields</a>.
|
||||||
|
|
||||||
## Type Hints with Metadata Annotations
|
## Type Hints with Metadata Annotations
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -192,7 +192,7 @@ Again, doing just that declaration, with **FastAPI** you get:
|
||||||
|
|
||||||
Apart from normal singular types like `str`, `int`, `float`, etc. you can use more complex singular types that inherit from `str`.
|
Apart from normal singular types like `str`, `int`, `float`, etc. you can use more complex singular types that inherit from `str`.
|
||||||
|
|
||||||
To see all the options you have, checkout the docs for <a href="https://docs.pydantic.dev/latest/concepts/types/" class="external-link" target="_blank">Pydantic's exotic types</a>. You will see some examples in the next chapter.
|
To see all the options you have, checkout <a href="https://docs.pydantic.dev/latest/concepts/types/" class="external-link" target="_blank">Pydantic's Type Overview</a>. You will see some examples in the next chapter.
|
||||||
|
|
||||||
For example, as in the `Image` model we have a `url` field, we can declare it to be an instance of Pydantic's `HttpUrl` instead of a `str`:
|
For example, as in the `Image` model we have a `url` field, we can declare it to be an instance of Pydantic's `HttpUrl` instead of a `str`:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ Here are some of the additional data types you can use:
|
||||||
* `datetime.timedelta`:
|
* `datetime.timedelta`:
|
||||||
* A Python `datetime.timedelta`.
|
* A Python `datetime.timedelta`.
|
||||||
* In requests and responses will be represented as a `float` of total seconds.
|
* In requests and responses will be represented as a `float` of total seconds.
|
||||||
* Pydantic also allows representing it as a "ISO 8601 time diff encoding", <a href="https://docs.pydantic.dev/latest/concepts/serialization/#json_encoders" class="external-link" target="_blank">see the docs for more info</a>.
|
* Pydantic also allows representing it as a "ISO 8601 time diff encoding", <a href="https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers" class="external-link" target="_blank">see the docs for more info</a>.
|
||||||
* `frozenset`:
|
* `frozenset`:
|
||||||
* In requests and responses, treated the same as a `set`:
|
* In requests and responses, treated the same as a `set`:
|
||||||
* In requests, a list will be read, eliminating duplicates and converting it to a `set`.
|
* In requests, a list will be read, eliminating duplicates and converting it to a `set`.
|
||||||
|
|
|
||||||
|
|
@ -500,7 +500,7 @@ To do that, you can declare that `None` is a valid type but still use `...` as t
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
Pydantic, which is what powers all the data validation and serialization in FastAPI, has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://docs.pydantic.dev/latest/concepts/models/#required-optional-fields" class="external-link" target="_blank">Required Optional fields</a>.
|
Pydantic, which is what powers all the data validation and serialization in FastAPI, has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://docs.pydantic.dev/2.3/usage/models/#required-fields" class="external-link" target="_blank">Required fields</a>.
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
Remember that in most of the cases, when something is required, you can simply omit the default, so you normally don't have to use `...`.
|
Remember that in most of the cases, when something is required, you can simply omit the default, so you normally don't have to use `...`.
|
||||||
|
|
|
||||||
|
|
@ -383,7 +383,7 @@ So, if you send a request to that *path operation* for the item with ID `foo`, t
|
||||||
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
|
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
|
||||||
|
|
||||||
!!! info
|
!!! info
|
||||||
FastAPI uses Pydantic model's `.dict()` with <a href="https://docs.pydantic.dev/latest/concepts/serialization/#modeldict" class="external-link" target="_blank">its `exclude_unset` parameter</a> to achieve this.
|
FastAPI uses Pydantic model's `.dict()` with <a href="https://docs.pydantic.dev/1.10/usage/exporting_models/#modeldict" class="external-link" target="_blank">its `exclude_unset` parameter</a> to achieve this.
|
||||||
|
|
||||||
!!! info
|
!!! info
|
||||||
You can also use:
|
You can also use:
|
||||||
|
|
@ -391,7 +391,7 @@ So, if you send a request to that *path operation* for the item with ID `foo`, t
|
||||||
* `response_model_exclude_defaults=True`
|
* `response_model_exclude_defaults=True`
|
||||||
* `response_model_exclude_none=True`
|
* `response_model_exclude_none=True`
|
||||||
|
|
||||||
as described in <a href="https://docs.pydantic.dev/latest/concepts/serialization/#modeldict" class="external-link" target="_blank">the Pydantic docs</a> for `exclude_defaults` and `exclude_none`.
|
as described in <a href="https://docs.pydantic.dev/1.10/usage/exporting_models/#modeldict" class="external-link" target="_blank">the Pydantic docs</a> for `exclude_defaults` and `exclude_none`.
|
||||||
|
|
||||||
#### Data with values for fields with defaults
|
#### Data with values for fields with defaults
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ That extra info will be added as-is to the output **JSON Schema** for that model
|
||||||
|
|
||||||
=== "Pydantic v2"
|
=== "Pydantic v2"
|
||||||
|
|
||||||
In Pydantic version 2, you would use the attribute `model_config`, that takes a `dict` as described in <a href="https://docs.pydantic.dev/latest/usage/model_config/" class="external-link" target="_blank">Pydantic's docs: Model Config</a>.
|
In Pydantic version 2, you would use the attribute `model_config`, that takes a `dict` as described in <a href="https://docs.pydantic.dev/latest/api/config/" class="external-link" target="_blank">Pydantic's docs: Configuration</a>.
|
||||||
|
|
||||||
You can set `"json_schema_extra"` with a `dict` containing any additional data you would like to show up in the generated JSON Schema, including `examples`.
|
You can set `"json_schema_extra"` with a `dict` containing any additional data you would like to show up in the generated JSON Schema, including `examples`.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue