@@ -67,9 +67,7 @@ We can use these relative imports because, for example, in the file `app.py` (th
6767
6868You could put all the database Models in a single Python module (a single Python file), for example ` models.py ` :
6969
70- ``` Python
71- {!./ docs_src/ tutorial/ code_structure/ tutorial001/ models.py!}
72- ```
70+ {* ./docs_src/tutorial/code_structure/tutorial001/models.py * }
7371
7472This way, you wouldn't have to deal with circular imports for other models.
7573
@@ -79,19 +77,15 @@ And then you could import the models from this file/module in any other file/mod
7977
8078Then you could put the code creating the ** engine** and the function to create all the tables (if you are not using migrations) in another file ` database.py ` :
8179
82- ``` Python
83- {!./ docs_src/ tutorial/ code_structure/ tutorial001/ database.py!}
84- ```
80+ {* ./docs_src/tutorial/code_structure/tutorial001/database.py * }
8581
8682This file would also be imported by your application code, to use the shared ** engine** and to get and call the function ` create_db_and_tables() ` .
8783
8884### Application File
8985
9086Finally, you could put the code to create the ** app** in another file ` app.py ` :
9187
92- ``` Python hl_lines="3-4"
93- {!./ docs_src/ tutorial/ code_structure/ tutorial001/ app.py!}
94- ```
88+ {* ./docs_src/tutorial/code_structure/tutorial001/app.py hl[ 3:4] * }
9589
9690Here we import the models, the engine, and the function to create all the tables and then we can use them all internally.
9791
@@ -207,29 +201,23 @@ So, we can use it in an `if` block and import things inside the `if` block. And
207201
208202Using that trick of ` TYPE_CHECKING ` we can "import" the ` Team ` in ` hero_model.py ` :
209203
210- ``` Python hl_lines="1 5-6 16"
211- {!./ docs_src/ tutorial/ code_structure/ tutorial002/ hero_model.py!}
212- ```
204+ {* ./docs_src/tutorial/code_structure/tutorial002/hero_model.py hl[ 1,5:6,16] * }
213205
214206Have in mind that now we * have* to put the annotation of ` Team ` as a string: ` "Team" ` , so that Python doesn't have errors at runtime.
215207
216208### Team Model File
217209
218210We use the same trick in the ` team_model.py ` file:
219211
220- ``` Python hl_lines="1 5-6 14"
221- {!./ docs_src/ tutorial/ code_structure/ tutorial002/ team_model.py!}
222- ```
212+ {* ./docs_src/tutorial/code_structure/tutorial002/team_model.py hl[ 1,5:6,14] * }
223213
224214Now we get editor support, autocompletion, inline errors, and ** SQLModel** keeps working. 🎉
225215
226216### App File
227217
228218Now, just for completeness, the ` app.py ` file would import the models from both modules:
229219
230- ``` Python hl_lines="4-5 10 12-14"
231- {!./ docs_src/ tutorial/ code_structure/ tutorial002/ app.py!}
232- ```
220+ {* ./docs_src/tutorial/code_structure/tutorial002/app.py hl[ 4:5,10,12:14] * }
233221
234222And of course, all the tricks with ` TYPE_CHECKING ` and type annotations in strings are ** only needed in the files with circular imports** .
235223
0 commit comments