From 3375d1cbd426c79854af9e32b3bf2c35ada3f3bd Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 16:49:42 +0530 Subject: [PATCH 1/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 63 +----------------------- 1 file changed, 2 insertions(+), 61 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 41c1ac6498..386af9faf4 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -108,74 +108,15 @@ And we want to have a `HeroPublic` with the `id` field, but this time annotated ## Multiple Models with Duplicated Fields The simplest way to solve it could be to create **multiple models**, each one with all the corresponding fields: - -//// tab | Python 3.10+ - -```Python hl_lines="5-9 12-15 18-22" -# This would work, but there's a better option below 🚨 - -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py[ln:5-22]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="5-9 12-15 18-22" -# This would work, but there's a better option below 🚨 - -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py[ln:7-24]!} - -# Code below omitted 👇 -``` - //// -//// tab | Python 3.7+ - -```Python hl_lines="5-9 12-15 18-22" -# This would work, but there's a better option below 🚨 - -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py[ln:7-24]!} - -# Code below omitted 👇 -``` +{*./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9, 12:15, 18:22] *} //// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py!} -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} /// From 2164f94116476bc8776a0bb0ae89d29796edc824 Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 16:54:59 +0530 Subject: [PATCH 2/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 92 ++---------------------- 1 file changed, 4 insertions(+), 88 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 386af9faf4..74e21cd091 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -110,7 +110,7 @@ And we want to have a `HeroPublic` with the `id` field, but this time annotated The simplest way to solve it could be to create **multiple models**, each one with all the corresponding fields: //// -{*./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9, 12:15, 18:22] *} +{*./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9,12:15,18:22] *} //// @@ -140,65 +140,13 @@ Let's now see how to use these new models in the FastAPI application. Let's first check how is the process to create a hero now: -//// tab | Python 3.10+ - -```Python hl_lines="3-4 6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py[ln:44-51]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-4 6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py[ln:46-53]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-4 6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py[ln:46-53]!} - -# Code below omitted 👇 -``` +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44:51] hl[3:4, 6] *} //// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py!} -``` +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} //// @@ -208,39 +156,7 @@ Let's check that in detail. Now we use the type annotation `HeroCreate` for the request JSON data in the `hero` parameter of the **path operation function**. -//// tab | Python 3.10+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py[ln:45]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py[ln:47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py[ln:47]!} - -# Code below omitted 👇 -``` +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[45] *} //// From b1a2173bec58d3688e803f550ad87b9735f84107 Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:02:31 +0530 Subject: [PATCH 3/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 323 +---------------------- 1 file changed, 11 insertions(+), 312 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 74e21cd091..c49ecaf67b 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -108,18 +108,13 @@ And we want to have a `HeroPublic` with the `id` field, but this time annotated ## Multiple Models with Duplicated Fields The simplest way to solve it could be to create **multiple models**, each one with all the corresponding fields: -//// {*./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9,12:15,18:22] *} -//// - /// details | 👀 Full file preview {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} -/// - Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`. This means that the class `Hero` represents a **table** in the database. It is both a **Pydantic** model and a **SQLAlchemy** model. @@ -140,16 +135,12 @@ Let's now see how to use these new models in the FastAPI application. Let's first check how is the process to create a hero now: -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44:51] hl[3:4, 6] *} - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44:51] hl[3:4,6] *} /// details | 👀 Full file preview {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} -//// - /// Let's check that in detail. @@ -172,41 +163,7 @@ In versions of **SQLModel** before `0.0.14` you would use the method `.from_orm( We can now create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. -//// tab | Python 3.10+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py[ln:47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py[ln:49]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py[ln:49]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[3] *} Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance. @@ -214,41 +171,7 @@ Because it is just refreshed, it has the `id` field set with a new ID taken from And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`: -//// tab | Python 3.10+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py[ln:44]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py[ln:46]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial001.py[ln:46]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44] hl[3] *} This will validate that all the data that we promised is there and will remove any data we didn't declare. @@ -300,67 +223,12 @@ We can see from above that they all share some **base** fields: So let's create a **base** model `HeroBase` that the others can inherit from: -//// tab | Python 3.10+ - -```Python hl_lines="3-6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py[ln:5-8]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-10]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-6" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py[ln:7-10]!} - -# Code below omitted 👇 -``` - +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:8] hl[3:6] *} //// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py!} -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} /// @@ -372,69 +240,11 @@ But now we can create the **other models inheriting from it**, they will all sha Let's start with the only **table model**, the `Hero`: -//// tab | Python 3.10+ - -```Python hl_lines="9-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py[ln:5-12]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-14]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py[ln:7-14]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[9:10] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} Notice that `Hero` now doesn't inherit from `SQLModel`, but from `HeroBase`. @@ -450,69 +260,13 @@ And those inherited fields will also be in the **autocompletion** and **inline e Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`. -//// tab | Python 3.10+ - -```Python hl_lines="4 6 9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py[ln:5-12]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="4 6 9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-14]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="4 6 9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py[ln:7-14]!} - -# Code below omitted 👇 -``` +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5-12] hl[4,6,9] *} //// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} This won't affect this parent **data model** `HeroBase`. @@ -606,69 +360,14 @@ Now let's check the `HeroPublic` model. This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID. -//// tab | Python 3.10+ +{*./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} -```Python hl_lines="17-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py[ln:5-20]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="17-18" -# Code above omitted 👆 {!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-22]!} -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="17-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py[ln:7-22]!} - -# Code below omitted 👇 -``` - -//// - /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} ## Review the Updated Docs UI From 42318988371839582d07302f785c34994aa23c8a Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:05:58 +0530 Subject: [PATCH 4/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 64 ++---------------------- 1 file changed, 3 insertions(+), 61 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index c49ecaf67b..bd3342bdae 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -260,7 +260,7 @@ And those inherited fields will also be in the **autocompletion** and **inline e Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`. -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5-12] hl[4,6,9] *} +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[4,6,9] *} //// @@ -278,67 +278,12 @@ Now let's see the `HeroCreate` model that will be used to define the data that w This is a fun one: -//// tab | Python 3.10+ +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:16] hl[13:14] *} -```Python hl_lines="13-14" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py[ln:5-16]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="13-14" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-18]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13-14" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py[ln:7-18]!} - -# Code below omitted 👇 -``` - -//// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002.py!} -``` - -//// +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} /// @@ -362,9 +307,6 @@ This one just declares that the `id` field is required when reading a hero from {*./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} - -{!./docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py[ln:7-22]!} - /// details | 👀 Full file preview {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} From c4fa9e81c77c1e71cf49e1fd31fb0035abcbc53e Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:07:27 +0530 Subject: [PATCH 5/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index bd3342bdae..b9f6956860 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -109,7 +109,7 @@ And we want to have a `HeroPublic` with the `id` field, but this time annotated The simplest way to solve it could be to create **multiple models**, each one with all the corresponding fields: -{*./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9,12:15,18:22] *} +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9,12:15,18:22] *} /// details | 👀 Full file preview @@ -305,7 +305,7 @@ Now let's check the `HeroPublic` model. This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID. -{*./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} +{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} /// details | 👀 Full file preview From c13e6570edbddb3f8f7bac42482392587a0371df Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:45:44 +0530 Subject: [PATCH 6/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index b9f6956860..ea61f396a9 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -115,6 +115,8 @@ The simplest way to solve it could be to create **multiple models**, each one wi {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} +/// + Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`. This means that the class `Hero` represents a **table** in the database. It is both a **Pydantic** model and a **SQLAlchemy** model. @@ -163,16 +165,24 @@ In versions of **SQLModel** before `0.0.14` you would use the method `.from_orm( We can now create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. +/// + {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[3] *} +/// + Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance. Because it is just refreshed, it has the `id` field set with a new ID taken from the database. And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`: +/// + {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44] hl[3] *} +/// + This will validate that all the data that we promised is there and will remove any data we didn't declare. /// tip @@ -222,8 +232,10 @@ We can see from above that they all share some **base** fields: * `age`, optional So let's create a **base** model `HeroBase` that the others can inherit from: +/// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:8] hl[3:6] *} + //// /// details | 👀 Full file preview @@ -240,12 +252,16 @@ But now we can create the **other models inheriting from it**, they will all sha Let's start with the only **table model**, the `Hero`: +/// + {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[9:10] *} /// details | 👀 Full file preview {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} +/// + Notice that `Hero` now doesn't inherit from `SQLModel`, but from `HeroBase`. And now we only declare one single field directly, the `id`, that here is `Optional[int]`, and is a `primary_key`. @@ -260,6 +276,8 @@ And those inherited fields will also be in the **autocompletion** and **inline e Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`. +/// + {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[4,6,9] *} //// @@ -277,9 +295,11 @@ But once the child model `Hero` (the actual **table model**) inherits those fiel Now let's see the `HeroCreate` model that will be used to define the data that we want to receive in the API when creating a new hero. This is a fun one: +/// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:16] hl[13:14] *} +/// /// details | 👀 Full file preview @@ -304,13 +324,18 @@ On top of that, we could easily decide in the future that we want to receive **m Now let's check the `HeroPublic` model. This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID. +/// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} +/// + /// details | 👀 Full file preview {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} +/// + ## Review the Updated Docs UI The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroPublic`. But now, we define them in a smarter way with inheritance. From 608d7d59bed8dcd94ce4052f5046bb7e1e2f8c9c Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:48:43 +0530 Subject: [PATCH 7/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index ea61f396a9..176833dbd6 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -115,7 +115,7 @@ The simplest way to solve it could be to create **multiple models**, each one wi {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *} -/// +//// Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`. @@ -169,7 +169,7 @@ We can now create a new `Hero` instance (the one for the database) and put it in {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[3] *} -/// +//// Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance. @@ -177,11 +177,11 @@ Because it is just refreshed, it has the `id` field set with a new ID taken from And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`: -/// +//// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44] hl[3] *} -/// +//// This will validate that all the data that we promised is there and will remove any data we didn't declare. @@ -232,7 +232,7 @@ We can see from above that they all share some **base** fields: * `age`, optional So let's create a **base** model `HeroBase` that the others can inherit from: -/// +//// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:8] hl[3:6] *} @@ -242,7 +242,7 @@ So let's create a **base** model `HeroBase` that the others can inherit from: {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} -/// +//// As you can see, this is *not* a **table model**, it doesn't have the `table = True` config. @@ -260,7 +260,7 @@ Let's start with the only **table model**, the `Hero`: {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} -/// +//// Notice that `Hero` now doesn't inherit from `SQLModel`, but from `HeroBase`. @@ -305,7 +305,7 @@ This is a fun one: {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} -/// +//// What's happening here? @@ -334,7 +334,7 @@ This one just declares that the `id` field is required when reading a hero from {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *} -/// +//// ## Review the Updated Docs UI From 2379d119ef8db725a7f48821dfebc06e3add2c2a Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:54:09 +0530 Subject: [PATCH 8/9] Update multiple-models.md --- docs/tutorial/fastapi/multiple-models.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 176833dbd6..0e7cf40b8b 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -165,8 +165,6 @@ In versions of **SQLModel** before `0.0.14` you would use the method `.from_orm( We can now create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. -/// - {* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[3] *} //// @@ -252,7 +250,7 @@ But now we can create the **other models inheriting from it**, they will all sha Let's start with the only **table model**, the `Hero`: -/// +//// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[9:10] *} @@ -276,8 +274,6 @@ And those inherited fields will also be in the **autocompletion** and **inline e Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`. -/// - {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[4,6,9] *} //// @@ -295,11 +291,10 @@ But once the child model `Hero` (the actual **table model**) inherits those fiel Now let's see the `HeroCreate` model that will be used to define the data that we want to receive in the API when creating a new hero. This is a fun one: -/// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:16] hl[13:14] *} -/// +//// /// details | 👀 Full file preview @@ -328,7 +323,7 @@ This one just declares that the `id` field is required when reading a hero from {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *} -/// +//// /// details | 👀 Full file preview From ea9f1915cae5897a0a2ef94f192cf22c5d0266d1 Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:56:47 +0530 Subject: [PATCH 9/9] Fixed --- docs/tutorial/fastapi/multiple-models.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 0e7cf40b8b..7505156014 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -319,7 +319,6 @@ On top of that, we could easily decide in the future that we want to receive **m Now let's check the `HeroPublic` model. This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID. -/// {* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *}