Skip to content

Commit c13e657

Browse files
Update multiple-models.md
1 parent c4fa9e8 commit c13e657

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/tutorial/fastapi/multiple-models.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ The simplest way to solve it could be to create **multiple models**, each one wi
115115

116116
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py *}
117117

118+
///
119+
118120
Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`.
119121

120122
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(
163165

164166
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.
165167

168+
///
169+
166170
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[3] *}
167171

172+
///
173+
168174
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.
169175

170176
Because it is just refreshed, it has the `id` field set with a new ID taken from the database.
171177

172178
And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`:
173179

180+
///
181+
174182
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44] hl[3] *}
175183

184+
///
185+
176186
This will validate that all the data that we promised is there and will remove any data we didn't declare.
177187

178188
/// tip
@@ -222,8 +232,10 @@ We can see from above that they all share some **base** fields:
222232
* `age`, optional
223233

224234
So let's create a **base** model `HeroBase` that the others can inherit from:
235+
///
225236

226237
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:8] hl[3:6] *}
238+
227239
////
228240

229241
/// details | 👀 Full file preview
@@ -240,12 +252,16 @@ But now we can create the **other models inheriting from it**, they will all sha
240252

241253
Let's start with the only **table model**, the `Hero`:
242254

255+
///
256+
243257
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[9:10] *}
244258

245259
/// details | 👀 Full file preview
246260

247261
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *}
248262

263+
///
264+
249265
Notice that `Hero` now doesn't inherit from `SQLModel`, but from `HeroBase`.
250266

251267
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
260276

261277
Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`.
262278

279+
///
280+
263281
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[4,6,9] *}
264282

265283
////
@@ -277,9 +295,11 @@ But once the child model `Hero` (the actual **table model**) inherits those fiel
277295
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.
278296

279297
This is a fun one:
298+
///
280299

281300
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:16] hl[13:14] *}
282301

302+
///
283303

284304
/// details | 👀 Full file preview
285305

@@ -304,13 +324,18 @@ On top of that, we could easily decide in the future that we want to receive **m
304324
Now let's check the `HeroPublic` model.
305325

306326
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.
327+
///
307328

308329
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[17:18] *}
309330

331+
///
332+
310333
/// details | 👀 Full file preview
311334

312335
{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py *}
313336

337+
///
338+
314339
## Review the Updated Docs UI
315340

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

0 commit comments

Comments
 (0)