diff --git a/docs/tutorial/fastapi/relationships.md b/docs/tutorial/fastapi/relationships.md index 4087dfca10..908b711468 100644 --- a/docs/tutorial/fastapi/relationships.md +++ b/docs/tutorial/fastapi/relationships.md @@ -44,185 +44,29 @@ It's because we declared the `HeroPublic` with only the same base fields of the And the same way, we declared the `TeamPublic` with only the same base fields of the `TeamBase` plus the `id`. But it doesn't include a field `heroes` for the **relationship attribute**. -//// tab | Python 3.10+ +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[5:7] hl[3:5,9:10,23:24] *} -```Python hl_lines="3-5 9-10 14-19 23-24" -# Code above omitted 👆 +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[20:21] hl[3:5,9:10,23:24] *} -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:5-7]!} +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[29:34] hl[3:5,9:10,23:24] *} -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:20-21]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:29-34]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:43-44]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-5 9-10 14-19 23-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:7-9]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:22-23]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:31-36]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:45-46]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-5 9-10 14-19 23-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:7-9]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:22-23]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:31-36]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:45-46]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[43:44] hl[3:5,9:10,23:24] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py *} Now, remember that FastAPI uses the `response_model` to validate and **filter** the response data? In this case, we used `response_model=TeamPublic` and `response_model=HeroPublic`, so FastAPI will use them to filter the response data, even if we return a **table model** that includes **relationship attributes**: -//// tab | Python 3.10+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:102-107]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:156-161]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[102:107] hl[3,8,12,17] *} -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:104-109]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:158-163]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:104-109]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:158-163]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[156:161] hl[3,8,12,17] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py *} ## Don't Include All the Data @@ -304,69 +148,12 @@ Let's add the models `HeroPublicWithTeam` and `TeamPublicWithHeroes`. We'll add them **after** the other models so that we can easily reference the previous models. -//// tab | Python 3.10+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:59-64]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:61-66]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:61-66]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[59:64] hl[3:4,7:8] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py!} -``` - -//// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py *} -/// These two models are very **simple in code**, but there's a lot happening here. Let's check it out. @@ -400,81 +187,13 @@ This will tell **FastAPI** to take the object that we return from the *path oper In the case of the hero, this tells FastAPI to extract the `team` too. And in the case of the team, to extract the list of `heroes` too. -//// tab | Python 3.10+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:111-116]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:165-170]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:113-118]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:167-172]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[111:116] hl[3,8,12,17] *} -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:113-118]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:167-172]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[165:170] hl[3,8,12,17] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py *} ## Check It Out in the Docs UI