diff --git a/docs/tutorial/relationship-attributes/back-populates.md b/docs/tutorial/relationship-attributes/back-populates.md index 0eaa7bb650..72af76e3e5 100644 --- a/docs/tutorial/relationship-attributes/back-populates.md +++ b/docs/tutorial/relationship-attributes/back-populates.md @@ -20,63 +20,7 @@ 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[1:19] hl[9,19] *} ## Read Data Objects @@ -84,69 +28,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-113]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:111] hl[5:7,9:11] *} /// tip @@ -158,69 +40,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-117]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:115] hl[13:15] *} Up to this point, it's all good. 😊 @@ -242,81 +62,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:119-123]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104] hl[8,12] *} The first important thing is, we *haven't committed* the hero yet, so accessing the list of heroes would not trigger an automatic refresh. @@ -356,81 +102,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:125-132]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104] hl[8:9,15] *} 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: @@ -462,141 +134,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:119-123]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} /// tip @@ -629,63 +167,7 @@ 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} The string in `back_populates` is the name of the attribute *in the other* model, that will reference *the current* model. @@ -693,69 +175,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:6-11]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[4:9] hl[8] *} The string in `back_populates="team"` refers to the attribute `team` in the class `Hero` (the other class). @@ -763,69 +183,7 @@ 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:14-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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[12:19] hl[10] *} /// tip @@ -850,66 +208,4 @@ So, `back_populates` would most probably be something like `"hero"` or `"heroes" -//// 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 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py[ln:29-41]!} - -# 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!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py ln[27:39] hl[3,10,13,15] *}