@@ -6,49 +6,7 @@ Let's see now how to create data with relationships using these new **relationsh
66
77Let's check the old code we used to create some heroes and teams:
88
9- //// tab | Python 3.10+
10-
11- ``` Python hl_lines="9 12 18 24"
12- # Code above omitted 👆
13-
14- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001_py310.py[ln:29 - 58 ]!}
15-
16- # Code below omitted 👇
17- ```
18-
19- ////
20-
21- //// tab | Python 3.7+
22-
23- ``` Python hl_lines="9 12 18 24"
24- # Code above omitted 👆
25-
26- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001.py[ln:31 - 60 ]!}
27-
28- # Code below omitted 👇
29- ```
30-
31- ////
32-
33- /// details | 👀 Full file preview
34-
35- //// tab | Python 3.10+
36-
37- ``` Python
38- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001_py310.py!}
39- ```
40-
41- ////
42-
43- //// tab | Python 3.7+
44-
45- ``` Python
46- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001.py!}
47- ```
48-
49- ////
50-
51- ///
9+ {* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[ 29:58] hl[ 9,12,18,24] * }
5210
5311There are several things to ** notice** here.
5412
@@ -68,69 +26,7 @@ This is the first area where these **relationship attributes** can help. 🤓
6826
6927Now let's do all that, but this time using the new, shiny ` Relationship ` attributes:
7028
71- //// tab | Python 3.10+
72-
73- ``` Python hl_lines="9 12 18"
74- # Code above omitted 👆
75-
76- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001_py310.py[ln:32 - 55 ]!}
77-
78- # Code below omitted 👇
79- ```
80-
81- ////
82-
83- //// tab | Python 3.9+
84-
85- ``` Python hl_lines="9 12 18"
86- # Code above omitted 👆
87-
88- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001_py39.py[ln:34 - 57 ]!}
89-
90- # Code below omitted 👇
91- ```
92-
93- ////
94-
95- //// tab | Python 3.7+
96-
97- ``` Python hl_lines="9 12 18"
98- # Code above omitted 👆
99-
100- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001.py[ln:34 - 57 ]!}
101-
102- # Code below omitted 👇
103- ```
104-
105- ////
106-
107- /// details | 👀 Full file preview
108-
109- //// tab | Python 3.10+
110-
111- ``` Python
112- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001_py310.py!}
113- ```
114-
115- ////
116-
117- //// tab | Python 3.9+
118-
119- ``` Python
120- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001_py39.py!}
121- ```
122-
123- ////
124-
125- //// tab | Python 3.7+
126-
127- ``` Python
128- {!./ docs_src/ tutorial/ relationship_attributes/ define_relationship_attributes/ tutorial001.py!}
129- ```
130-
131- ////
132-
133- ///
29+ {* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[ 32:55] hl[ 9,12,18] * }
13430
13531Now 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 ` .
13632
@@ -146,163 +42,15 @@ And then, as you can see, we only have to do one `commit()`.
14642
14743The 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 ` :
14844
149- //// tab | Python 3.10+
150-
151- ``` Python hl_lines="8"
152- # Code above omitted 👆
153-
154- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:32 - 33 ]!}
155-
156- # Previous code here omitted 👈
157-
158- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:57 - 61 ]!}
159-
160- # Code below omitted 👇
161- ```
162-
163- ////
164-
165- //// tab | Python 3.9+
166-
167- ``` Python hl_lines="8"
168- # Code above omitted 👆
169-
170- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:34 - 35 ]!}
171-
172- # Previous code here omitted 👈
173-
174- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:59 - 63 ]!}
175-
176- # Code below omitted 👇
177- ```
178-
179- ////
180-
181- //// tab | Python 3.7+
182-
183- ``` Python hl_lines="8"
184- # Code above omitted 👆
185-
186- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:34 - 35 ]!}
187-
188- # Previous code here omitted 👈
189-
190- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:59 - 63 ]!}
191-
192- # Code below omitted 👇
193- ```
194-
195- ////
196-
197- /// details | 👀 Full file preview
198-
199- //// tab | Python 3.10+
200-
201- ``` Python
202- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py!}
203- ```
204-
205- ////
206-
207- //// tab | Python 3.9+
208-
209- ``` Python
210- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py!}
211- ```
212-
213- ////
214-
215- //// tab | Python 3.7+
216-
217- ``` Python
218- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py!}
219- ```
220-
221- ////
222-
223- ///
45+ {* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[ 32:33] hl[ 8] * }
22446
22547## Create a Team with Heroes
22648
22749Before, we created some ` Team ` instances and passed them in the ` team= ` argument when creating ` Hero ` instances.
22850
22951We could also create the ` Hero ` instances first, and then pass them in the ` heroes= ` argument that takes a list, when creating a ` Team ` instance:
23052
231- //// tab | Python 3.10+
232-
233- ``` Python hl_lines="13 15-16"
234- # Code above omitted 👆
235-
236- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:32 - 33 ]!}
237-
238- # Previous code here omitted 👈
239-
240- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:63 - 73 ]!}
241-
242- # Code below omitted 👇
243- ```
244-
245- ////
246-
247- //// tab | Python 3.9+
248-
249- ``` Python hl_lines="13 15-16"
250- # Code above omitted 👆
251-
252- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:34 - 35 ]!}
253-
254- # Previous code here omitted 👈
255-
256- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:65 - 75 ]!}
257-
258- # Code below omitted 👇
259- ```
260-
261- ////
262-
263- //// tab | Python 3.7+
264-
265- ``` Python hl_lines="13 15-16"
266- # Code above omitted 👆
267-
268- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:34 - 35 ]!}
269-
270- # Previous code here omitted 👈
271-
272- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:65 - 75 ]!}
273-
274- # Code below omitted 👇
275- ```
276-
277- ////
278-
279- /// details | 👀 Full file preview
280-
281- //// tab | Python 3.10+
282-
283- ``` Python
284- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py!}
285- ```
286-
287- ////
288-
289- //// tab | Python 3.9+
290-
291- ``` Python
292- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py!}
293- ```
294-
295- ////
296-
297- //// tab | Python 3.7+
298-
299- ``` Python
300- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py!}
301- ```
302-
303- ////
304-
305- ///
53+ {* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[ 32:33] hl[ 13,15:16] * }
30654
30755Here we create two heroes first, ** Black Lion** and ** Princess Sure-E** , and then we pass them in the ` heroes ` argument.
30856
@@ -318,81 +66,7 @@ As the attribute `team.heroes` behaves like a list, we can simply append to it.
31866
31967Let's create some more heroes and add them to the ` team_preventers.heroes ` list attribute:
32068
321- //// tab | Python 3.10+
322-
323- ``` Python hl_lines="14-18"
324- # Code above omitted 👆
325-
326- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:32 - 33 ]!}
327-
328- # Previous code here omitted 👈
329-
330- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py[ln:75 - 91 ]!}
331-
332- # Code below omitted 👇
333- ```
334-
335- ////
336-
337- //// tab | Python 3.9+
338-
339- ``` Python hl_lines="14-18"
340- # Code above omitted 👆
341-
342- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:34 - 35 ]!}
343-
344- # Previous code here omitted 👈
345-
346- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py[ln:77 - 93 ]!}
347-
348- # Code below omitted 👇
349- ```
350-
351- ////
352-
353- //// tab | Python 3.7+
354-
355- ``` Python hl_lines="14-18"
356- # Code above omitted 👆
357-
358- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:34 - 35 ]!}
359-
360- # Previous code here omitted 👈
361-
362- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py[ln:77 - 93 ]!}
363-
364- # Code below omitted 👇
365- ```
366-
367- ////
368-
369- /// details | 👀 Full file preview
370-
371- //// tab | Python 3.10+
372-
373- ``` Python
374- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py310.py!}
375- ```
376-
377- ////
378-
379- //// tab | Python 3.9+
380-
381- ``` Python
382- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001_py39.py!}
383- ```
384-
385- ////
386-
387- //// tab | Python 3.7+
388-
389- ``` Python
390- {!./ docs_src/ tutorial/ relationship_attributes/ create_and_update_relationships/ tutorial001.py!}
391- ```
392-
393- ////
394-
395- ///
69+ {* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[ 32:33] hl[ 14:18] * }
39670
39771The 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.
39872
0 commit comments