@@ -4,33 +4,7 @@ Now we'll see how to update and remove these **many-to-many** relationships.
44
55We'll continue from where we left off with the previous code.
66
7- /// details | 👀 Full file preview
8-
9- //// tab | Python 3.10+
10-
11- ``` Python
12- {!./ docs_src/ tutorial/ many_to_many/ tutorial001_py310.py!}
13- ```
14-
15- ////
16-
17- //// tab | Python 3.9+
18-
19- ``` Python
20- {!./ docs_src/ tutorial/ many_to_many/ tutorial001_py39.py!}
21- ```
22-
23- ////
24-
25- //// tab | Python 3.7+
26-
27- ``` Python
28- {!./ docs_src/ tutorial/ many_to_many/ tutorial001.py!}
29- ```
30-
31- ////
32-
33- ///
7+ {* ./docs_src/tutorial/many_to_many/tutorial001_py310.py * }
348
359## Get Data to Update
3610
@@ -42,205 +16,19 @@ As you already know how these goes, I'll use the **short version** and get the d
4216
4317And because we are now using ` select() ` , we also have to import it.
4418
45- //// tab | Python 3.10+
46-
47- ``` Python hl_lines="1 5-10"
48- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py[ln:1 ]!}
49-
50- # Some code here omitted 👈
51-
52- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py[ln:72 - 77 ]!}
53-
54- # Code below omitted 👇
55- ```
56-
57- ////
58-
59- //// tab | Python 3.9+
60-
61- ``` Python hl_lines="3 7-12"
62- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py[ln:1 - 3 ]!}
63-
64- # Some code here omitted 👈
65-
66- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py[ln:78 - 83 ]!}
67-
68- # Code below omitted 👇
69- ```
70-
71- ////
72-
73- //// tab | Python 3.7+
74-
75- ``` Python hl_lines="3 7-12"
76- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py[ln:1 - 3 ]!}
77-
78- # Some code here omitted 👈
79-
80- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py[ln:78 - 83 ]!}
81-
82- # Code below omitted 👇
83- ```
84-
85- ////
86-
87- /// details | 👀 Full file preview
88-
89- //// tab | Python 3.10+
90-
91- ``` Python
92- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py!}
93- ```
94-
95- ////
96-
97- //// tab | Python 3.9+
98-
99- ``` Python
100- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py!}
101- ```
102-
103- ////
104-
105- //// tab | Python 3.7+
106-
107- ``` Python
108- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py!}
109- ```
110-
111- ////
112-
113- ///
19+ {* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[ 1] hl[ 1,5:10] * }
11420
11521And of course, we have to add ` update_heroes() ` to our ` main() ` function:
11622
117- //// tab | Python 3.10+
118-
119- ``` Python hl_lines="6"
120- # Code above omitted 👆
121-
122- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py[ln:94 - 101 ]!}
123- ```
124-
125- ////
126-
127- //// tab | Python 3.9+
128-
129- ``` Python hl_lines="6"
130- # Code above omitted 👆
131-
132- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py[ln:100 - 107 ]!}
133- ```
134-
135- ////
136-
137- //// tab | Python 3.7+
138-
139- ``` Python hl_lines="6"
140- # Code above omitted 👆
141-
142- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py[ln:100 - 107 ]!}
143- ```
144-
145- ////
146-
147- /// details | 👀 Full file preview
148-
149- //// tab | Python 3.10+
150-
151- ``` Python
152- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py!}
153- ```
154-
155- ////
156-
157- //// tab | Python 3.9+
158-
159- ``` Python
160- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py!}
161- ```
162-
163- ////
164-
165- //// tab | Python 3.7+
166-
167- ``` Python
168- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py!}
169- ```
170-
171- ////
172-
173- ///
23+ {* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[ 94:101] hl[ 6] * }
17424
17525## Add Many-to-Many Relationships
17626
17727Now let's imagine that ** Spider-Boy** thinks that the ** Z-Force** team is super cool and decides to go there and join them.
17828
17929We can use the same ** relationship attributes** to include ` hero_spider_boy ` in the ` team_z_force.heroes ` .
18030
181- //// tab | Python 3.10+
182-
183- ``` Python hl_lines="10-12 14-15"
184- # Code above omitted 👆
185-
186- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py[ln:72 - 84 ]!}
187-
188- # Code below omitted 👇
189- ```
190-
191- ////
192-
193- //// tab | Python 3.9+
194-
195- ``` Python hl_lines="10-12 14-15"
196- # Code above omitted 👆
197-
198- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py[ln:78 - 90 ]!}
199-
200- # Code below omitted 👇
201- ```
202-
203- ////
204-
205- //// tab | Python 3.7+
206-
207- ``` Python hl_lines="10-12 14-15"
208- # Code above omitted 👆
209-
210- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py[ln:78 - 90 ]!}
211-
212- # Code below omitted 👇
213- ```
214-
215- ////
216-
217- /// details | 👀 Full file preview
218-
219- //// tab | Python 3.10+
220-
221- ``` Python
222- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py!}
223- ```
224-
225- ////
226-
227- //// tab | Python 3.9+
228-
229- ``` Python
230- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py!}
231- ```
232-
233- ////
234-
235- //// tab | Python 3.7+
236-
237- ``` Python
238- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py!}
239- ```
240-
241- ////
242-
243- ///
31+ {* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[ 72:84] hl[ 10:12,14:15] * }
24432
24533/// tip
24634
@@ -325,69 +113,7 @@ Because `hero_spider_boy.teams` is just a list (a special list managed by SQLAlc
325113
326114In this case, we use the method ` .remove() ` , that takes an item and removes it from the list.
327115
328- //// tab | Python 3.10+
329-
330- ``` Python hl_lines="17-19 21-22"
331- # Code above omitted 👆
332-
333- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py[ln:72 - 91 ]!}
334-
335- # Code below omitted 👇
336- ```
337-
338- ////
339-
340- //// tab | Python 3.9+
341-
342- ``` Python hl_lines="17-19 21-22"
343- # Code above omitted 👆
344-
345- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py[ln:78 - 97 ]!}
346-
347- # Code below omitted 👇
348- ```
349-
350- ////
351-
352- //// tab | Python 3.7+
353-
354- ``` Python hl_lines="17-19 21-22"
355- # Code above omitted 👆
356-
357- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py[ln:78 - 97 ]!}
358-
359- # Code below omitted 👇
360- ```
361-
362- ////
363-
364- /// details | 👀 Full file preview
365-
366- //// tab | Python 3.10+
367-
368- ``` Python
369- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py310.py!}
370- ```
371-
372- ////
373-
374- //// tab | Python 3.9+
375-
376- ``` Python
377- {!./ docs_src/ tutorial/ many_to_many/ tutorial002_py39.py!}
378- ```
379-
380- ////
381-
382- //// tab | Python 3.7+
383-
384- ``` Python
385- {!./ docs_src/ tutorial/ many_to_many/ tutorial002.py!}
386- ```
387-
388- ////
389-
390- ///
116+ {* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[ 72:91] hl[ 17:19,21:22] * }
391117
392118And 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 ` .
393119
0 commit comments