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.
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
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
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.
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:
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.
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:
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
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
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