@@ -67,9 +67,7 @@ We can use these relative imports because, for example, in the file `app.py` (th
67
67
68
68
You could put all the database Models in a single Python module (a single Python file), for example ` models.py ` :
69
69
70
- ``` Python
71
- {!./ docs_src/ tutorial/ code_structure/ tutorial001/ models.py!}
72
- ```
70
+ {* ./docs_src/tutorial/code_structure/tutorial001/models.py * }
73
71
74
72
This way, you wouldn't have to deal with circular imports for other models.
75
73
@@ -79,19 +77,15 @@ And then you could import the models from this file/module in any other file/mod
79
77
80
78
Then 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 ` :
81
79
82
- ``` Python
83
- {!./ docs_src/ tutorial/ code_structure/ tutorial001/ database.py!}
84
- ```
80
+ {* ./docs_src/tutorial/code_structure/tutorial001/database.py * }
85
81
86
82
This 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() ` .
87
83
88
84
### Application File
89
85
90
86
Finally, you could put the code to create the ** app** in another file ` app.py ` :
91
87
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] * }
95
89
96
90
Here we import the models, the engine, and the function to create all the tables and then we can use them all internally.
97
91
@@ -207,29 +201,23 @@ So, we can use it in an `if` block and import things inside the `if` block. And
207
201
208
202
Using that trick of ` TYPE_CHECKING ` we can "import" the ` Team ` in ` hero_model.py ` :
209
203
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] * }
213
205
214
206
Have 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.
215
207
216
208
### Team Model File
217
209
218
210
We use the same trick in the ` team_model.py ` file:
219
211
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] * }
223
213
224
214
Now we get editor support, autocompletion, inline errors, and ** SQLModel** keeps working. 🎉
225
215
226
216
### App File
227
217
228
218
Now, just for completeness, the ` app.py ` file would import the models from both modules:
229
219
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] * }
233
221
234
222
And of course, all the tricks with ` TYPE_CHECKING ` and type annotations in strings are ** only needed in the files with circular imports** .
235
223
0 commit comments