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

@@ -10,16 +10,62 @@ We'll continue from where we left off with the previous code.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
## Create Heroes
As we have done before, we'll create a function `create_heroes()` and we'll create some teams and heroes in it:
//// tab | Python 3.10+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:36-54]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:42-60]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
# Code above omitted 👆
@@ -28,12 +74,34 @@ As we have done before, we'll create a function `create_heroes()` and we'll crea
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
This is very similar to what we have done before.
@@ -48,6 +116,32 @@ See how **Deadpond** now belongs to the two teams?
Now let's do as we have done before, `commit` the **session**, `refresh` the data, and print it:
//// tab | Python 3.10+
```Python hl_lines="22-25 27-29 31-36"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:36-69]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="22-25 27-29 31-36"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:42-75]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="22-25 27-29 31-36"
# Code above omitted 👆
@@ -56,19 +150,67 @@ Now let's do as we have done before, `commit` the **session**, `refresh` the dat
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
## Add to Main
As before, add the `create_heroes()` function to the `main()` function to make sure it is called when running this program from the command line:
```Python hl_lines="22-25 27-29 31-36"
//// tab | Python 3.10+
```Python
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:72-74]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:78-80]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:78-80]!}
@@ -76,12 +218,34 @@ As before, add the `create_heroes()` function to the `main()` function to make s
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
## Run the Program

View File

@@ -12,18 +12,62 @@ As we want to support a **many-to-many** relationship, now we need a **link tabl
We can create it just as any other **SQLModel**:
//// tab | Python 3.10+
```Python hl_lines="4-6"
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:1-6]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="6-12"
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:1-12]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6-12"
{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:1-12]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
This is a **SQLModel** class model table like any other.
@@ -38,6 +82,32 @@ And **both fields are primary keys**. We hadn't used this before. 🤓
Let's see the `Team` model, it's almost identical as before, but with a little change:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:9-14]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:15-20]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
@@ -46,12 +116,34 @@ Let's see the `Team` model, it's almost identical as before, but with a little c
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
The **relationship attribute `heroes`** is still a list of heroes, annotated as `List["Hero"]`. Again, we use `"Hero"` in quotes because we haven't declared that class yet by this point in the code (but as you know, editors and **SQLModel** understand that).
@@ -66,6 +158,32 @@ And here's the important part to allow the **many-to-many** relationship, we use
Let's see the other side, here's the `Hero` model:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:17-23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:23-29]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
@@ -74,12 +192,34 @@ Let's see the other side, here's the `Hero` model:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
We **removed** the previous `team_id` field (column) because now the relationship is done via the link table. 🔥
@@ -98,6 +238,32 @@ And now we have a **`link_model=HeroTeamLink`**. ✨
The same as before, we will have the rest of the code to create the **engine**, and a function to create all the tables `create_db_and_tables()`.
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:26-33]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:32-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
@@ -106,17 +272,67 @@ The same as before, we will have the rest of the code to create the **engine**,
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
And as in previous examples, we will add that function to a function `main()`, and we will call that `main()` function in the main block:
//// tab | Python 3.10+
```Python hl_lines="4"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:72-73]!}
# We will do more stuff here later 👈
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:77-78]!}
```
////
//// tab | Python 3.9+
```Python hl_lines="4"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:78-79]!}
# We will do more stuff here later 👈
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:83-84]!}
```
////
//// tab | Python 3.7+
```Python hl_lines="4"
# Code above omitted 👆
@@ -126,12 +342,34 @@ And as in previous examples, we will add that function to a function `main()`, a
{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:83-84]!}
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///

View File

@@ -32,6 +32,32 @@ We will add a new field `is_training`.
And we will also add two **relationship attributes**, for the linked `team` and `hero`:
//// tab | Python 3.10+
```Python hl_lines="6 8-9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:4-10]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="10 12-13"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:6-16]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="10 12-13"
# Code above omitted 👆
@@ -40,12 +66,34 @@ And we will also add two **relationship attributes**, for the linked `team` and
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
The new **relationship attributes** have their own `back_populates` pointing to new relationship attributes we will create in the `Hero` and `Team` models:
@@ -67,6 +115,32 @@ Now let's update the `Team` model.
We no longer have the `heroes` relationship attribute, and instead we have the new `hero_links` attribute:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:13-18]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:19-24]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
@@ -75,12 +149,34 @@ We no longer have the `heroes` relationship attribute, and instead we have the n
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
## Update Hero Model
@@ -89,6 +185,32 @@ The same with the `Hero` model.
We change the `teams` relationship attribute for `team_links`:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:21-27]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:27-33]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
@@ -97,12 +219,34 @@ We change the `teams` relationship attribute for `team_links`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
## Create Relationships
@@ -111,6 +255,32 @@ Now the process to create relationships is very similar.
But now we create the **explicit link models** manually, pointing to their hero and team instances, and specifying the additional link data (`is_training`):
//// tab | Python 3.10+
```Python hl_lines="21-30 32-35"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:40-79]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="21-30 32-35"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:46-85]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="21-30 32-35"
# Code above omitted 👆
@@ -119,12 +289,34 @@ But now we create the **explicit link models** manually, pointing to their hero
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
We are just adding the link model instances to the session, because the link model instances are connected to the heroes and teams, they will be also automatically included in the session when we commit.
@@ -223,6 +415,32 @@ Now, to add a new relationship, we have to create a new `HeroTeamLink` instance
Here we do that in the `update_heroes()` function:
//// tab | Python 3.10+
```Python hl_lines="10-15"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:82-97]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="10-15"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:88-103]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="10-15"
# Code above omitted 👆
@@ -231,12 +449,34 @@ Here we do that in the `update_heroes()` function:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
## Run the Program with the New Relationship
@@ -318,6 +558,40 @@ So now we want to update the status of `is_training` to `False`.
We can do that by iterating on the links:
//// tab | Python 3.10+
```Python hl_lines="8-10"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:82-83]!}
# Code here omitted 👈
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:99-107]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8-10"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:88-89]!}
# Code here omitted 👈
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:105-113]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8-10"
# Code above omitted 👆
@@ -330,12 +604,34 @@ We can do that by iterating on the links:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial003.py!}
```
////
///
## Run the Program with the Updated Relationships

View File

@@ -6,10 +6,30 @@ We'll continue from where we left off with the previous code.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial001.py!}
```
////
///
## Get Data to Update
@@ -22,6 +42,36 @@ As you already know how these goes, I'll use the **short version** and get the d
And because we are now using `select()`, we also have to import it.
//// tab | Python 3.10+
```Python hl_lines="1 5-10"
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:1]!}
# Some code here omitted 👈
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-77]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3 7-12"
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:1-3]!}
# Some code here omitted 👈
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-83]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3 7-12"
{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:1-3]!}
@@ -32,28 +82,94 @@ And because we are now using `select()`, we also have to import it.
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002.py!}
```
////
///
And of course, we have to add `update_heroes()` to our `main()` function:
//// tab | Python 3.10+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:94-101]!}
```
////
//// tab | Python 3.9+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:100-107]!}
```
////
//// tab | Python 3.7+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:100-107]!}
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002.py!}
```
////
///
## Add Many-to-Many Relationships
@@ -62,6 +178,32 @@ Now let's imagine that **Spider-Boy** thinks that the **Z-Force** team is super
We can use the same **relationship attributes** to include `hero_spider_boy` in the `team_z_force.heroes`.
//// tab | Python 3.10+
```Python hl_lines="10-12 14-15"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-84]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="10-12 14-15"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-90]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="10-12 14-15"
# Code above omitted 👆
@@ -70,12 +212,34 @@ We can use the same **relationship attributes** to include `hero_spider_boy` in
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002.py!}
```
////
///
/// tip
@@ -161,6 +325,32 @@ Because `hero_spider_boy.teams` is just a list (a special list managed by SQLAlc
In this case, we use the method `.remove()`, that takes an item and removes it from the list.
//// tab | Python 3.10+
```Python hl_lines="17-19 21-22"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-91]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="17-19 21-22"
# Code above omitted 👆
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-97]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="17-19 21-22"
# Code above omitted 👆
@@ -169,12 +359,34 @@ In this case, we use the method `.remove()`, that takes an item and removes it f
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/many_to_many/tutorial002.py!}
```
////
///
And this time, just to show again that by using `back_populates` **SQLModel** (actually SQLAlchemy) takes care of connecting the models by their relationships, even though we performed the operation from the `hero_spider_boy` object (modifying `hero_spider_boy.teams`), we are adding `team_z_force` to the **session**. And we commit that, without even add `hero_spider_boy`.