From: Yurii Motov Date: Wed, 24 Dec 2025 17:46:20 +0000 (+0100) Subject: Update `docs_src/tutorial/code_structure` X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d43821274ca1016f39fec831109e14edc21e0b3;p=thirdparty%2Ffastapi%2Fsqlmodel.git Update `docs_src/tutorial/code_structure` --- diff --git a/docs/tutorial/code-structure.md b/docs/tutorial/code-structure.md index 6e377b89..08a41654 100644 --- a/docs/tutorial/code-structure.md +++ b/docs/tutorial/code-structure.md @@ -67,9 +67,7 @@ We can use these relative imports because, for example, in the file `app.py` (th You could put all the database Models in a single Python module (a single Python file), for example `models.py`: -```Python -{!./docs_src/tutorial/code_structure/tutorial001/models.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial001_py310/models.py *} This way, you wouldn't have to deal with circular imports for other models. @@ -79,9 +77,7 @@ And then you could import the models from this file/module in any other file/mod 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`: -```Python -{!./docs_src/tutorial/code_structure/tutorial001/database.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial001_py310/database.py *} 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()`. @@ -89,9 +85,7 @@ This file would also be imported by your application code, to use the shared **e Finally, you could put the code to create the **app** in another file `app.py`: -```Python hl_lines="3-4" -{!./docs_src/tutorial/code_structure/tutorial001/app.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial001_py310/app.py hl[3:4] *} Here we import the models, the engine, and the function to create all the tables and then we can use them all internally. @@ -207,9 +201,7 @@ So, we can use it in an `if` block and import things inside the `if` block. And Using that trick of `TYPE_CHECKING` we can "import" the `Team` in `hero_model.py`: -```Python hl_lines="1 5-6 16" -{!./docs_src/tutorial/code_structure/tutorial002/hero_model.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial002_py310/hero_model.py hl[1,5:6,16] *} 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. @@ -217,9 +209,7 @@ Have in mind that now we *have* to put the annotation of `Team` as a string: `"T We use the same trick in the `team_model.py` file: -```Python hl_lines="1 5-6 14" -{!./docs_src/tutorial/code_structure/tutorial002/team_model.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial002_py310/team_model.py hl[1,5:6,14] *} Now we get editor support, autocompletion, inline errors, and **SQLModel** keeps working. 🎉 @@ -227,9 +217,7 @@ Now we get editor support, autocompletion, inline errors, and **SQLModel** keeps Now, just for completeness, the `app.py` file would import the models from both modules: -```Python hl_lines="4-5 10 12-14" -{!./docs_src/tutorial/code_structure/tutorial002/app.py!} -``` +{* ./docs_src/tutorial/code_structure/tutorial002_py310/app.py hl[4:5,10,12:14] *} And of course, all the tricks with `TYPE_CHECKING` and type annotations in strings are **only needed in the files with circular imports**. diff --git a/docs_src/tutorial/code_structure/tutorial001/__init__.py b/docs_src/tutorial/code_structure/tutorial001/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/docs_src/tutorial/code_structure/tutorial001/app.py b/docs_src/tutorial/code_structure/tutorial001/app.py deleted file mode 100644 index 3d1bfc69..00000000 --- a/docs_src/tutorial/code_structure/tutorial001/app.py +++ /dev/null @@ -1,29 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .models import Hero, Team - - -def create_heroes(): - with Session(engine) as session: - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", secret_name="Dive Wilson", team=team_z_force - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial001/database.py b/docs_src/tutorial/code_structure/tutorial001/database.py deleted file mode 100644 index d6de16c1..00000000 --- a/docs_src/tutorial/code_structure/tutorial001/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial001/models.py b/docs_src/tutorial/code_structure/tutorial001/models.py deleted file mode 100644 index 8e2647b3..00000000 --- a/docs_src/tutorial/code_structure/tutorial001/models.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, SQLModel - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") diff --git a/docs_src/tutorial/code_structure/tutorial002/__init__.py b/docs_src/tutorial/code_structure/tutorial002/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/docs_src/tutorial/code_structure/tutorial002/app.py b/docs_src/tutorial/code_structure/tutorial002/app.py deleted file mode 100644 index 2ecaec0c..00000000 --- a/docs_src/tutorial/code_structure/tutorial002/app.py +++ /dev/null @@ -1,30 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .hero_model import Hero -from .team_model import Team - - -def create_heroes(): - with Session(engine) as session: - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", secret_name="Dive Wilson", team=team_z_force - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial002/database.py b/docs_src/tutorial/code_structure/tutorial002/database.py deleted file mode 100644 index d6de16c1..00000000 --- a/docs_src/tutorial/code_structure/tutorial002/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial002/hero_model.py b/docs_src/tutorial/code_structure/tutorial002/hero_model.py deleted file mode 100644 index 06dd9c6d..00000000 --- a/docs_src/tutorial/code_structure/tutorial002/hero_model.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TYPE_CHECKING, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .team_model import Team - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional["Team"] = Relationship(back_populates="heroes") diff --git a/docs_src/tutorial/code_structure/tutorial002/team_model.py b/docs_src/tutorial/code_structure/tutorial002/team_model.py deleted file mode 100644 index c8a008bf..00000000 --- a/docs_src/tutorial/code_structure/tutorial002/team_model.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import TYPE_CHECKING, List, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .hero_model import Hero - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") diff --git a/tests/test_tutorial/test_code_structure/test_tutorial001.py b/tests/test_tutorial/test_code_structure/test_tutorial001.py index 99ae5c00..aa832f62 100644 --- a/tests/test_tutorial/test_code_structure/test_tutorial001.py +++ b/tests/test_tutorial/test_code_structure/test_tutorial001.py @@ -5,7 +5,7 @@ from types import ModuleType import pytest from sqlmodel import create_engine -from tests.conftest import PrintMock, needs_py39, needs_py310 +from tests.conftest import PrintMock, needs_py310 expected_calls = [ [ @@ -34,8 +34,7 @@ class Modules: @pytest.fixture( name="modules", params=[ - "tutorial001", - pytest.param("tutorial001_py39", marks=needs_py39), + pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), ], ) diff --git a/tests/test_tutorial/test_code_structure/test_tutorial002.py b/tests/test_tutorial/test_code_structure/test_tutorial002.py index f1d4043e..990d3eca 100644 --- a/tests/test_tutorial/test_code_structure/test_tutorial002.py +++ b/tests/test_tutorial/test_code_structure/test_tutorial002.py @@ -5,7 +5,7 @@ from types import ModuleType import pytest from sqlmodel import create_engine -from ...conftest import PrintMock, needs_py39, needs_py310 +from ...conftest import PrintMock, needs_py310 expected_calls = [ [ @@ -34,8 +34,7 @@ class Modules: @pytest.fixture( name="modules", params=[ - "tutorial002", - pytest.param("tutorial002_py39", marks=needs_py39), + pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), ], )