mirror of https://github.com/tiangolo/fastapi.git
📝 Update includes in `docs/en/docs/tutorial/body-nested-models.md` (#12812)
This commit is contained in:
parent
4dcdb20151
commit
069e9bdea8
|
|
@ -6,21 +6,7 @@ With **FastAPI**, you can define, validate, document, and use arbitrarily deeply
|
|||
|
||||
You can define an attribute to be a subtype. For example, a Python `list`:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="12"
|
||||
{!> ../../docs_src/body_nested_models/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="14"
|
||||
{!> ../../docs_src/body_nested_models/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial001_py310.py hl[12] *}
|
||||
|
||||
This will make `tags` be a list, although it doesn't declare the type of the elements of the list.
|
||||
|
||||
|
|
@ -34,9 +20,7 @@ In Python 3.9 and above you can use the standard `list` to declare these type an
|
|||
|
||||
But in Python versions before 3.9 (3.6 and above), you first need to import `List` from standard Python's `typing` module:
|
||||
|
||||
```Python hl_lines="1"
|
||||
{!> ../../docs_src/body_nested_models/tutorial002.py!}
|
||||
```
|
||||
{* ../../docs_src/body_nested_models/tutorial002.py hl[1] *}
|
||||
|
||||
### Declare a `list` with a type parameter
|
||||
|
||||
|
|
@ -65,29 +49,7 @@ Use that same standard syntax for model attributes with internal types.
|
|||
|
||||
So, in our example, we can make `tags` be specifically a "list of strings":
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="12"
|
||||
{!> ../../docs_src/body_nested_models/tutorial002_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="14"
|
||||
{!> ../../docs_src/body_nested_models/tutorial002_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="14"
|
||||
{!> ../../docs_src/body_nested_models/tutorial002.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial002_py310.py hl[12] *}
|
||||
|
||||
## Set types
|
||||
|
||||
|
|
@ -97,29 +59,7 @@ And Python has a special data type for sets of unique items, the `set`.
|
|||
|
||||
Then we can declare `tags` as a set of strings:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="12"
|
||||
{!> ../../docs_src/body_nested_models/tutorial003_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="14"
|
||||
{!> ../../docs_src/body_nested_models/tutorial003_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="1 14"
|
||||
{!> ../../docs_src/body_nested_models/tutorial003.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial003_py310.py hl[12] *}
|
||||
|
||||
With this, even if you receive a request with duplicate data, it will be converted to a set of unique items.
|
||||
|
||||
|
|
@ -141,57 +81,13 @@ All that, arbitrarily nested.
|
|||
|
||||
For example, we can define an `Image` model:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="7-9"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="9-11"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="9-11"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial004_py310.py hl[7:9] *}
|
||||
|
||||
### Use the submodel as a type
|
||||
|
||||
And then we can use it as the type of an attribute:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="18"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="20"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="20"
|
||||
{!> ../../docs_src/body_nested_models/tutorial004.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial004_py310.py hl[18] *}
|
||||
|
||||
This would mean that **FastAPI** would expect a body similar to:
|
||||
|
||||
|
|
@ -224,29 +120,7 @@ To see all the options you have, checkout <a href="https://docs.pydantic.dev/lat
|
|||
|
||||
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`:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="2 8"
|
||||
{!> ../../docs_src/body_nested_models/tutorial005_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="4 10"
|
||||
{!> ../../docs_src/body_nested_models/tutorial005_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="4 10"
|
||||
{!> ../../docs_src/body_nested_models/tutorial005.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial005_py310.py hl[2,8] *}
|
||||
|
||||
The string will be checked to be a valid URL, and documented in JSON Schema / OpenAPI as such.
|
||||
|
||||
|
|
@ -254,29 +128,7 @@ The string will be checked to be a valid URL, and documented in JSON Schema / Op
|
|||
|
||||
You can also use Pydantic models as subtypes of `list`, `set`, etc.:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="18"
|
||||
{!> ../../docs_src/body_nested_models/tutorial006_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="20"
|
||||
{!> ../../docs_src/body_nested_models/tutorial006_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="20"
|
||||
{!> ../../docs_src/body_nested_models/tutorial006.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial006_py310.py hl[18] *}
|
||||
|
||||
This will expect (convert, validate, document, etc.) a JSON body like:
|
||||
|
||||
|
|
@ -314,29 +166,7 @@ Notice how the `images` key now has a list of image objects.
|
|||
|
||||
You can define arbitrarily deeply nested models:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="7 12 18 21 25"
|
||||
{!> ../../docs_src/body_nested_models/tutorial007_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="9 14 20 23 27"
|
||||
{!> ../../docs_src/body_nested_models/tutorial007_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="9 14 20 23 27"
|
||||
{!> ../../docs_src/body_nested_models/tutorial007.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial007_py310.py hl[7,12,18,21,25] *}
|
||||
|
||||
/// info
|
||||
|
||||
|
|
@ -360,21 +190,7 @@ images: list[Image]
|
|||
|
||||
as in:
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="13"
|
||||
{!> ../../docs_src/body_nested_models/tutorial008_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="15"
|
||||
{!> ../../docs_src/body_nested_models/tutorial008.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial008_py39.py hl[13] *}
|
||||
|
||||
## Editor support everywhere
|
||||
|
||||
|
|
@ -404,21 +220,7 @@ That's what we are going to see here.
|
|||
|
||||
In this case, you would accept any `dict` as long as it has `int` keys with `float` values:
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="7"
|
||||
{!> ../../docs_src/body_nested_models/tutorial009_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="9"
|
||||
{!> ../../docs_src/body_nested_models/tutorial009.py!}
|
||||
```
|
||||
|
||||
////
|
||||
{* ../../docs_src/body_nested_models/tutorial009_py39.py hl[7] *}
|
||||
|
||||
/// tip
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue