@@ -37,24 +37,9 @@ We will continue with the code in the previous example and we will add more thin
3737
3838/// details | 👀 Full file preview
3939
40- //// tab | Python 3.10+
41-
42- ``` Python
43- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001_py310.py!}
44- ```
45-
46- ////
47-
48- //// tab | Python 3.7+
49-
50- ``` Python
51- {!./ docs_src/ tutorial/ connect/ insert/ tutorial001.py!}
52- ```
53-
54- ////
40+ {* ./docs_src/tutorial/connect/insert/tutorial001_py310.py * }
5541
5642///
57-
5843## ` SELECT ` Connected Data with SQL
5944
6045Let's start seeing how SQL works when selecting connected data. This is where SQL databases actually shine.
@@ -135,49 +120,7 @@ Remember SQLModel's `select()` function? It can take more than one argument.
135120
136121So, we can pass the ` Hero ` and ` Team ` model classes. And we can also use both their columns in the ` .where() ` part:
137122
138- //// tab | Python 3.10+
139-
140- ``` Python hl_lines="5"
141- # Code above omitted 👆
142-
143- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py[ln:61 - 63 ]!}
144-
145- # Code below omitted 👇
146- ```
147-
148- ////
149-
150- //// tab | Python 3.7+
151-
152- ``` Python hl_lines="5"
153- # Code above omitted 👆
154-
155- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py[ln:63 - 65 ]!}
156-
157- # Code below omitted 👇
158- ```
159-
160- ////
161-
162- /// details | 👀 Full file preview
163-
164- //// tab | Python 3.10+
165-
166- ``` Python
167- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py!}
168- ```
169-
170- ////
171-
172- //// tab | Python 3.7+
173-
174- ``` Python
175- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py!}
176- ```
177-
178- ////
179-
180- ///
123+ {* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[ 61:63] hl[ 63] * }
181124
182125Notice that in the comparison with ` == ` we are using the class attributes for both ` Hero.team_id ` and ` Team.id ` .
183126
@@ -187,49 +130,7 @@ Now we can execute it and get the `results` object.
187130
188131And as we used ` select ` with two models, we will receive tuples of instances of those two models, so we can iterate over them naturally in a ` for ` loop:
189132
190- //// tab | Python 3.10+
191-
192- ``` Python hl_lines="7"
193- # Code above omitted 👆
194-
195- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py[ln:61 - 66 ]!}
196-
197- # Code below omitted 👇
198- ```
199-
200- ////
201-
202- //// tab | Python 3.7+
203-
204- ``` Python hl_lines="7"
205- # Code above omitted 👆
206-
207- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py[ln:63 - 68 ]!}
208-
209- # Code below omitted 👇
210- ```
211-
212- ////
213-
214- /// details | 👀 Full file preview
215-
216- //// tab | Python 3.10+
217-
218- ``` Python
219- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py!}
220- ```
221-
222- ////
223-
224- //// tab | Python 3.7+
225-
226- ``` Python
227- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py!}
228- ```
229-
230- ////
231-
232- ///
133+ {* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[ 61:66] hl[ 65] * }
233134
234135For each iteration in the ` for ` loop we get a a tuple with an instance of the class ` Hero ` and an instance of the class ` Team ` .
235136
@@ -247,50 +148,7 @@ And you should get autocompletion and inline errors in your editor for both `her
247148
248149As always, we must remember to add this new ` select_heroes() ` function to the ` main() ` function to make sure it is executed when we call this program from the command line.
249150
250- //// tab | Python 3.10+
251-
252- ``` Python hl_lines="6"
253- # Code above omitted 👆
254-
255- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py[ln:69 - 72 ]!}
256-
257- # Code below omitted 👇
258- ```
259-
260- ////
261-
262- //// tab | Python 3.7+
263-
264- ``` Python hl_lines="6"
265- # Code above omitted 👆
266-
267- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py[ln:71 - 74 ]!}
268-
269- # Code below omitted 👇
270- ```
271-
272- ////
273-
274- /// details | 👀 Full file preview
275-
276- //// tab | Python 3.10+
277-
278- ``` Python
279- {!./ docs_src/ tutorial/ connect/ select/ tutorial001_py310.py!}
280- ```
281-
282- ////
283-
284- //// tab | Python 3.7+
285-
286- ``` Python
287- {!./ docs_src/ tutorial/ connect/ select/ tutorial001.py!}
288- ```
289-
290- ////
291-
292- ///
293-
151+ {* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[ 69:72] hl[ 72] * }
294152
295153## Run the Program
296154
@@ -396,49 +254,7 @@ The same way there's a `.where()` available when using `select()`, there's also
396254
397255And in SQLModel (actually SQLAlchemy), when using the ` .join() ` , because we already declared what is the ` foreign_key ` when creating the models, we don't have to pass an ` ON ` part, it is inferred automatically:
398256
399- //// tab | Python 3.10+
400-
401- ``` Python hl_lines="5"
402- # Code above omitted 👆
403-
404- {!./ docs_src/ tutorial/ connect/ select/ tutorial002_py310.py[ln:61 - 66 ]!}
405-
406- # Code below omitted 👇
407- ```
408-
409- ////
410-
411- //// tab | Python 3.7+
412-
413- ``` Python hl_lines="5"
414- # Code above omitted 👆
415-
416- {!./ docs_src/ tutorial/ connect/ select/ tutorial002.py[ln:63 - 68 ]!}
417-
418- # Code below omitted 👇
419- ```
420-
421- ////
422-
423- /// details | 👀 Full file preview
424-
425- //// tab | Python 3.10+
426-
427- ``` Python
428- {!./ docs_src/ tutorial/ connect/ select/ tutorial002_py310.py!}
429- ```
430-
431- ////
432-
433- //// tab | Python 3.7+
434-
435- ``` Python
436- {!./ docs_src/ tutorial/ connect/ select/ tutorial002.py!}
437- ```
438-
439- ////
440-
441- ///
257+ {* ./docs_src/tutorial/connect/select/tutorial002_py310.py ln[ 61:66] hl[ 63] * }
442258
443259Also notice that we are still including ` Team ` in the ` select(Hero, Team) ` , because we still want to access that data.
444260
@@ -565,49 +381,7 @@ Now let's replicate the same query in **SQLModel**.
565381
566382` .join() ` has a parameter we can use ` isouter=True ` to make the ` JOIN ` be a ` LEFT OUTER JOIN ` :
567383
568- //// tab | Python 3.10+
569-
570- ``` Python hl_lines="5"
571- # Code above omitted 👆
572-
573- {!./ docs_src/ tutorial/ connect/ select/ tutorial003_py310.py[ln:61 - 66 ]!}
574-
575- # Code below omitted 👇
576- ```
577-
578- ////
579-
580- //// tab | Python 3.7+
581-
582- ``` Python hl_lines="5"
583- # Code above omitted 👆
584-
585- {!./ docs_src/ tutorial/ connect/ select/ tutorial003.py[ln:63 - 68 ]!}
586-
587- # Code below omitted 👇
588- ```
589-
590- ////
591-
592- /// details | 👀 Full file preview
593-
594- //// tab | Python 3.10+
595-
596- ``` Python
597- {!./ docs_src/ tutorial/ connect/ select/ tutorial003_py310.py!}
598- ```
599-
600- ////
601-
602- //// tab | Python 3.7+
603-
604- ``` Python
605- {!./ docs_src/ tutorial/ connect/ select/ tutorial003.py!}
606- ```
607-
608- ////
609-
610- ///
384+ {* ./docs_src/tutorial/connect/select/tutorial003_py310.py ln[ 61:66] hl[ 63] * }
611385
612386And if we run it, it will output:
613387
@@ -654,49 +428,7 @@ But we would still be able to **filter** the rows with it. 🤓
654428
655429We could even add some additional ` .where() ` after ` .join() ` to filter the data more, for example to return only the heroes from one team:
656430
657- //// tab | Python 3.10+
658-
659- ``` Python hl_lines="5"
660- # Code above omitted 👆
661-
662- {!./ docs_src/ tutorial/ connect/ select/ tutorial004_py310.py[ln:61 - 66 ]!}
663-
664- # Code below omitted 👇
665- ```
666-
667- ////
668-
669- //// tab | Python 3.7+
670-
671- ``` Python hl_lines="5"
672- # Code above omitted 👆
673-
674- {!./ docs_src/ tutorial/ connect/ select/ tutorial004.py[ln:63 - 68 ]!}
675-
676- # Code below omitted 👇
677- ```
678-
679- ////
680-
681- /// details | 👀 Full file preview
682-
683- //// tab | Python 3.10+
684-
685- ``` Python
686- {!./ docs_src/ tutorial/ connect/ select/ tutorial004_py310.py!}
687- ```
688-
689- ////
690-
691- //// tab | Python 3.7+
692-
693- ``` Python
694- {!./ docs_src/ tutorial/ connect/ select/ tutorial004.py!}
695- ```
696-
697- ////
698-
699- ///
431+ {* ./docs_src/tutorial/connect/select/tutorial004_py310.py ln[ 61:66] hl[ 63] * }
700432
701433Here we are ** filtering** with ` .where() ` to get only the heroes that belong to the ** Preventers** team.
702434
@@ -727,49 +459,7 @@ Preventer Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48
727459
728460By putting the ` Team ` in ` select() ` we tell ** SQLModel** and the database that we want the team data too.
729461
730- //// tab | Python 3.10+
731-
732- ``` Python hl_lines="5"
733- # Code above omitted 👆
734-
735- {!./ docs_src/ tutorial/ connect/ select/ tutorial005_py310.py[ln:61 - 66 ]!}
736-
737- # Code below omitted 👇
738- ```
739-
740- ////
741-
742- //// tab | Python 3.7+
743-
744- ``` Python hl_lines="5"
745- # Code above omitted 👆
746-
747- {!./ docs_src/ tutorial/ connect/ select/ tutorial005.py[ln:63 - 68 ]!}
748-
749- # Code below omitted 👇
750- ```
751-
752- ////
753-
754- /// details | 👀 Full file preview
755-
756- //// tab | Python 3.10+
757-
758- ``` Python
759- {!./ docs_src/ tutorial/ connect/ select/ tutorial005_py310.py!}
760- ```
761-
762- ////
763-
764- //// tab | Python 3.7+
765-
766- ``` Python
767- {!./ docs_src/ tutorial/ connect/ select/ tutorial005.py!}
768- ```
769-
770- ////
771-
772- ///
462+ {* ./docs_src/tutorial/connect/select/tutorial005_py310.py ln[ 61:66] hl[ 63] * }
773463
774464And if we run that, it will output:
775465
0 commit comments