You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/multiple-models.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -98,7 +98,7 @@ But we also want to have a `HeroCreate` for the data we want to receive when **c
98
98
*`secret_name`, required
99
99
*`age`, optional
100
100
101
-
And we want to have a `HeroRead` with the `id` field, but this time annotated with `id: int`, instead of `id: Optional[int]`, to make it clear that it is required in responses **read** from the clients:
101
+
And we want to have a `HeroPublic` with the `id` field, but this time annotated with `id: int`, instead of `id: Optional[int]`, to make it clear that it is required in responses **read** from the clients:
102
102
103
103
*`id`, required
104
104
*`name`, required
@@ -183,9 +183,9 @@ Here's the important detail, and probably the most important feature of **SQLMod
183
183
184
184
This means that the class `Hero` represents a **table** in the database. It is both a **Pydantic** model and a **SQLAlchemy** model.
185
185
186
-
But `HeroCreate` and `HeroRead` don't have `table = True`. They are only **data models**, they are only **Pydantic** models. They won't be used with the database, but only to declare data schemas for the API (or for other uses).
186
+
But `HeroCreate` and `HeroPublic` don't have `table = True`. They are only **data models**, they are only **Pydantic** models. They won't be used with the database, but only to declare data schemas for the API (or for other uses).
187
187
188
-
This also means that `SQLModel.metadata.create_all()` won't create tables in the database for `HeroCreate` and `HeroRead`, because they don't have `table = True`, which is exactly what we want. 🚀
188
+
This also means that `SQLModel.metadata.create_all()` won't create tables in the database for `HeroCreate` and `HeroPublic`, because they don't have `table = True`, which is exactly what we want. 🚀
189
189
190
190
/// tip
191
191
@@ -355,7 +355,7 @@ Then we just `add` it to the **session**, `commit`, and `refresh` it, and finall
355
355
356
356
Because it is just refreshed, it has the `id` field set with a new ID taken from the database.
357
357
358
-
And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroRead`:
358
+
And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`:
359
359
360
360
//// tab | Python 3.10+
361
361
@@ -743,9 +743,9 @@ As an alternative, we could use `HeroBase` directly in the API code instead of `
743
743
744
744
On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example, a password), and now we already have the class to put those extra fields.
745
745
746
-
### The `HeroRead`**Data Model**
746
+
### The `HeroPublic`**Data Model**
747
747
748
-
Now let's check the `HeroRead` model.
748
+
Now let's check the `HeroPublic` model.
749
749
750
750
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.
751
751
@@ -815,7 +815,7 @@ This one just declares that the `id` field is required when reading a hero from
815
815
816
816
## Review the Updated Docs UI
817
817
818
-
The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroRead`. But now, we define them in a smarter way with inheritance.
818
+
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.
819
819
820
820
So, we can jump to the docs UI right away and see how they look with the updated data.
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/relationships.md
+10-10
Original file line number
Diff line number
Diff line change
@@ -40,9 +40,9 @@ Let's update that. 🤓
40
40
41
41
First, why is it that we are not getting the related data for each hero and for each team?
42
42
43
-
It's because we declared the `HeroRead` with only the same base fields of the `HeroBase` plus the `id`. But it doesn't include a field `team` for the **relationship attribute**.
43
+
It's because we declared the `HeroPublic` with only the same base fields of the `HeroBase` plus the `id`. But it doesn't include a field `team` for the **relationship attribute**.
44
44
45
-
And the same way, we declared the `TeamRead` with only the same base fields of the `TeamBase` plus the `id`. But it doesn't include a field `heroes` for the **relationship attribute**.
45
+
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**.
46
46
47
47
//// tab | Python 3.10+
48
48
@@ -146,7 +146,7 @@ And the same way, we declared the `TeamRead` with only the same base fields of t
146
146
147
147
Now, remember that <ahref="https://fastapi.tiangolo.com/tutorial/response-model/"class="external-link"target="_blank">FastAPI uses the `response_model` to validate and **filter** the response data</a>?
148
148
149
-
In this case, we used `response_model=TeamRead` and `response_model=HeroRead`, so FastAPI will use them to filter the response data, even if we return a **table model** that includes **relationship attributes**:
149
+
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**:
150
150
151
151
//// tab | Python 3.10+
152
152
@@ -300,7 +300,7 @@ Let's add a couple more **data models** that declare that data so we can use the
300
300
301
301
## Models with Relationships
302
302
303
-
Let's add the models `HeroReadWithTeam` and `TeamReadWithHeroes`.
303
+
Let's add the models `HeroPublicWithTeam` and `TeamPublicWithHeroes`.
304
304
305
305
We'll add them **after** the other models so that we can easily reference the previous models.
306
306
@@ -372,11 +372,11 @@ These two models are very **simple in code**, but there's a lot happening here.
372
372
373
373
### Inheritance and Type Annotations
374
374
375
-
The `HeroReadWithTeam`**inherits** from `HeroRead`, which means that it will have the **normal fields for reading**, including the required `id` that was declared in `HeroRead`.
375
+
The `HeroPublicWithTeam`**inherits** from `HeroPublic`, which means that it will have the **normal fields for reading**, including the required `id` that was declared in `HeroPublic`.
376
376
377
-
And then it adds the **new field**`team`, which could be `None`, and is declared with the type `TeamRead` with the base fields for reading a team.
377
+
And then it adds the **new field**`team`, which could be `None`, and is declared with the type `TeamPublic` with the base fields for reading a team.
378
378
379
-
Then we do the same for the `TeamReadWithHeroes`, it **inherits** from `TeamRead`, and declares the **new field**`heroes`, which is a list of `HeroRead`.
379
+
Then we do the same for the `TeamPublicWithHeroes`, it **inherits** from `TeamPublic`, and declares the **new field**`heroes`, which is a list of `HeroPublic`.
380
380
381
381
### Data Models Without Relationship Attributes
382
382
@@ -386,11 +386,11 @@ Instead, here these are only **data models** that will tell FastAPI **which attr
386
386
387
387
### Reference to Other Models
388
388
389
-
Also, notice that the field `team` is not declared with this new `TeamReadWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamRead` model.
389
+
Also, notice that the field `team` is not declared with this new `TeamPublicWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamPublic` model.
390
390
391
-
And the same for `TeamReadWithHeroes`, the model used for the new field `heroes` uses `HeroRead` to get only each hero's data.
391
+
And the same for `TeamPublicWithHeroes`, the model used for the new field `heroes` uses `HeroPublic` to get only each hero's data.
392
392
393
-
This also means that, even though we have these two new models, **we still need the previous ones**, `HeroRead` and `TeamRead`, because we need to reference them here (and we are also using them in the rest of the *path operations*).
393
+
This also means that, even though we have these two new models, **we still need the previous ones**, `HeroPublic` and `TeamPublic`, because we need to reference them here (and we are also using them in the rest of the *path operations*).
0 commit comments