Add source examples for Python 3.10 and 3.9 with updated syntax (#842)

Co-authored-by: Esteban Maya Cadavid <emayacadavid9@gmail.com>
This commit is contained in:
Sebastián Ramírez
2024-03-21 17:49:38 -05:00
committed by GitHub
parent 4c3f242ae2
commit 9141c8a920
39 changed files with 7456 additions and 25 deletions

View File

@@ -20,18 +20,62 @@ Let's understand that better with an example.
Let's see how that works by writing an **incomplete** version first, without `back_populates`:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
## Read Data Objects
@@ -40,6 +84,32 @@ Now, we will get the **Spider-Boy** hero and, *independently*, the **Preventers*
As you already know how this works, I won't separate that in a select `statement`, `results`, etc. Let's use the shorter form in a single call:
//// tab | Python 3.10+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-111]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-113]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
@@ -48,12 +118,34 @@ As you already know how this works, I won't separate that in a select `statement
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
/// tip
@@ -66,6 +158,32 @@ When writing your own code, this is probably the style you will use most often,
Now, let's print the current **Spider-Boy**, the current **Preventers** team, and particularly, the current **Preventers** list of heroes:
//// tab | Python 3.10+
```Python hl_lines="13-15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-115]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="13-15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-117]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="13-15"
# Code above omitted 👆
@@ -74,12 +192,34 @@ Now, let's print the current **Spider-Boy**, the current **Preventers** team, an
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
Up to this point, it's all good. 😊
@@ -102,6 +242,40 @@ Notice that we have **Spider-Boy** there.
Now let's update **Spider-Boy**, removing him from the team by setting `hero_spider_boy.team = None` and then let's print this object again:
//// tab | Python 3.10+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8 12"
# Code above omitted 👆
@@ -114,12 +288,34 @@ Now let's update **Spider-Boy**, removing him from the team by setting `hero_spi
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
The first important thing is, we *haven't committed* the hero yet, so accessing the list of heroes would not trigger an automatic refresh.
@@ -160,6 +356,40 @@ Oh, no! 😱 **Spider-Boy** is still listed there!
Now, if we commit it and print again:
//// tab | Python 3.10+
```Python hl_lines="8-9 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:123-130]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8-9 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:125-132]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8-9 15"
# Code above omitted 👆
@@ -172,12 +402,34 @@ Now, if we commit it and print again:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
When we access `preventers_team.heroes` after the `commit`, that triggers a refresh, so we get the latest list, without **Spider-Boy**, so that's fine again:
@@ -210,22 +462,100 @@ That's what `back_populates` is for. ✨
Let's add it back:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
And we can keep the rest of the code the same:
//// tab | Python 3.10+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8 12"
# Code above omitted 👆
@@ -238,12 +568,34 @@ And we can keep the rest of the code the same:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
/// tip
@@ -277,18 +629,62 @@ Now that you know why `back_populates` is there, let's review the exact value ag
It's quite simple code, it's just a string, but it might be confusing to think exactly *what* string should go there:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
The string in `back_populates` is the name of the attribute *in the other* model, that will reference *the current* model.
@@ -297,6 +693,32 @@ The string in `back_populates` is the name of the attribute *in the other* model
So, in the class `Team`, we have an attribute `heroes` and we declare it with `Relationship(back_populates="team")`.
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:4-9]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:6-11]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
@@ -305,12 +727,34 @@ So, in the class `Team`, we have an attribute `heroes` and we declare it with `R
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
The string in `back_populates="team"` refers to the attribute `team` in the class `Hero` (the other class).
@@ -319,6 +763,32 @@ And, in the class `Hero`, we declare an attribute `team`, and we declare it with
So, the string `"heroes"` refers to the attribute `heroes` in the class `Team`.
//// tab | Python 3.10+
```Python hl_lines="10"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:12-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="10"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:14-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="10"
# Code above omitted 👆
@@ -327,12 +797,34 @@ So, the string `"heroes"` refers to the attribute `heroes` in the class `Team`.
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
/// tip
@@ -358,6 +850,32 @@ So, `back_populates` would most probably be something like `"hero"` or `"heroes"
<img src="/img/tutorial/relationships/attributes/back-populates2.svg">
//// tab | Python 3.10+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py[ln:27-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py[ln:29-41]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
@@ -366,10 +884,32 @@ So, `back_populates` would most probably be something like `"hero"` or `"heroes"
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py!}
```
////
///

View File

@@ -6,6 +6,20 @@ Let's see now how to create data with relationships using these new **relationsh
Let's check the old code we used to create some heroes and teams:
//// tab | Python 3.10+
```Python hl_lines="9 12 18 24"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-58]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9 12 18 24"
# Code above omitted 👆
@@ -14,12 +28,26 @@ Let's check the old code we used to create some heroes and teams:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
There are several things to **notice** here.
@@ -40,6 +68,32 @@ This is the first area where these **relationship attributes** can help. 🤓
Now let's do all that, but this time using the new, shiny `Relationship` attributes:
//// tab | Python 3.10+
```Python hl_lines="9 12 18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:32-55]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9 12 18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:34-57]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9 12 18"
# Code above omitted 👆
@@ -48,12 +102,34 @@ Now let's do all that, but this time using the new, shiny `Relationship` attribu
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`.
@@ -70,6 +146,40 @@ And then, as you can see, we only have to do one `commit()`.
The same way we could assign an integer with a `team.id` to a `hero.team_id`, we can also assign the `Team` instance to the `hero.team`:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:57-61]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:59-63]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
@@ -82,12 +192,34 @@ The same way we could assign an integer with a `team.id` to a `hero.team_id`, we
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
## Create a Team with Heroes
@@ -96,6 +228,40 @@ Before, we created some `Team` instances and passed them in the `team=` argument
We could also create the `Hero` instances first, and then pass them in the `heroes=` argument that takes a list, when creating a `Team` instance:
//// tab | Python 3.10+
```Python hl_lines="13 15-16"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:63-73]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="13 15-16"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:65-75]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="13 15-16"
# Code above omitted 👆
@@ -108,12 +274,34 @@ We could also create the `Hero` instances first, and then pass them in the `hero
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
Here we create two heroes first, **Black Lion** and **Princess Sure-E**, and then we pass them in the `heroes` argument.
@@ -130,6 +318,40 @@ As the attribute `team.heroes` behaves like a list, we can simply append to it.
Let's create some more heroes and add them to the `team_preventers.heroes` list attribute:
//// tab | Python 3.10+
```Python hl_lines="14-18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:75-91]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="14-18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:77-93]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="14-18"
# Code above omitted 👆
@@ -142,12 +364,34 @@ Let's create some more heroes and add them to the `team_preventers.heroes` list
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
The attribute `team_preventers.heroes` behaves like a list. But it's a special type of list, because when we modify it adding heroes to it, **SQLModel** (actually SQLAlchemy) **keeps track of the necessary changes** to be done in the database.

View File

@@ -41,18 +41,44 @@ Now that you know how these tables work underneath and how the model classes rep
Up to now, we have only used the `team_id` column to connect the tables when querying with `select()`:
//// tab | Python 3.10+
```Python hl_lines="16"
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:1-16]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="18"
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:1-18]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
This is a **plain field** like all the others, all representing a **column in the table**.
@@ -61,34 +87,122 @@ But now let's add a couple of new special attributes to these model classes, let
First, import `Relationship` from `sqlmodel`:
//// tab | Python 3.10+
```Python hl_lines="1"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-3]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-3]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
Next, use that `Relationship` to declare a new attribute in the model classes:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
## What Are These Relationship Attributes

View File

@@ -6,6 +6,40 @@ Now that we know how to connect data using **relationship Attributes**, let's se
First, add a function `select_heroes()` where we get a hero to start working with, and add that function to the `main()` function:
//// tab | Python 3.10+
```Python hl_lines="3-7 14"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:108-111]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3-7 14"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:110-113]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3-7 14"
# Code above omitted 👆
@@ -18,12 +52,34 @@ First, add a function `select_heroes()` where we get a hero to start working wit
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
## Select the Related Team - Old Way
@@ -32,6 +88,32 @@ Now that we have a hero, we can get the team this hero belongs to.
With what we have learned **up to now**, we could use a `select()` statement, then execute it with `session.exec()`, and then get the `.first()` result, for example:
//// tab | Python 3.10+
```Python hl_lines="9-12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-103]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9-12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-105]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9-12"
# Code above omitted 👆
@@ -40,12 +122,34 @@ With what we have learned **up to now**, we could use a `select()` statement, th
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
## Get Relationship Team - New Way
@@ -54,6 +158,40 @@ But now that we have the **relationship attributes**, we can just access them, a
So, the highlighted block above, has the same results as the block below:
//// tab | Python 3.10+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!}
# Code from the previous example omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:105]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!}
# Code from the previous example omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:107]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
# Code above omitted 👆
@@ -66,12 +204,34 @@ So, the highlighted block above, has the same results as the block below:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
/// tip
@@ -86,6 +246,32 @@ For example, here, **inside** a `with` block with a `Session` object.
And the same way, when we are working on the **many** side of the **one-to-many** relationship, we can get a list of of the related objects just by accessing the relationship attribute:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:94-100]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:96-102]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
@@ -94,12 +280,34 @@ And the same way, when we are working on the **many** side of the **one-to-many*
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
That would print a list with all the heroes in the Preventers team:

View File

@@ -8,6 +8,32 @@ And then for some reason needs to leave the **Preventers** for some years. 😭
We can remove the relationship by setting it to `None`, the same as with the `team_id`, it also works with the new relationship attribute `.team`:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:103-114]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:105-116]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
@@ -16,16 +42,64 @@ We can remove the relationship by setting it to `None`, the same as with the `te
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
And of course, we should remember to add this `update_heroes()` function to `main()` so that it runs when we call this program from the command line:
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
@@ -34,12 +108,34 @@ And of course, we should remember to add this `update_heroes()` function to `mai
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
## Recap

View File

@@ -2,18 +2,62 @@
In the first Relationship attribute, we declare it with `List["Hero"]`, putting the `Hero` in quotes instead of just normally there:
//// tab | Python 3.10+
```Python hl_lines="9"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
What's that about? Can't we just write it normally as `List[Hero]`?