]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
Update `docs_src/tutorial/code_structure`
authorYurii Motov <yurii.motov.monte@gmail.com>
Wed, 24 Dec 2025 17:46:20 +0000 (18:46 +0100)
committerYurii Motov <yurii.motov.monte@gmail.com>
Thu, 25 Dec 2025 09:31:55 +0000 (10:31 +0100)
12 files changed:
docs/tutorial/code-structure.md
docs_src/tutorial/code_structure/tutorial001/__init__.py [deleted file]
docs_src/tutorial/code_structure/tutorial001/app.py [deleted file]
docs_src/tutorial/code_structure/tutorial001/database.py [deleted file]
docs_src/tutorial/code_structure/tutorial001/models.py [deleted file]
docs_src/tutorial/code_structure/tutorial002/__init__.py [deleted file]
docs_src/tutorial/code_structure/tutorial002/app.py [deleted file]
docs_src/tutorial/code_structure/tutorial002/database.py [deleted file]
docs_src/tutorial/code_structure/tutorial002/hero_model.py [deleted file]
docs_src/tutorial/code_structure/tutorial002/team_model.py [deleted file]
tests/test_tutorial/test_code_structure/test_tutorial001.py
tests/test_tutorial/test_code_structure/test_tutorial002.py

index 6e377b89e4a2228f2ef0db1dfd396ae8b1d1adf8..08a4165429ed7d1c0cc2a94bed7bb1fd4f36de8b 100644 (file)
@@ -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 (file)
index e69de29..0000000
diff --git a/docs_src/tutorial/code_structure/tutorial001/app.py b/docs_src/tutorial/code_structure/tutorial001/app.py
deleted file mode 100644 (file)
index 3d1bfc6..0000000
+++ /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 (file)
index d6de16c..0000000
+++ /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 (file)
index 8e2647b..0000000
+++ /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 (file)
index e69de29..0000000
diff --git a/docs_src/tutorial/code_structure/tutorial002/app.py b/docs_src/tutorial/code_structure/tutorial002/app.py
deleted file mode 100644 (file)
index 2ecaec0..0000000
+++ /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 (file)
index d6de16c..0000000
+++ /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 (file)
index 06dd9c6..0000000
+++ /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 (file)
index c8a008b..0000000
+++ /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")
index 99ae5c00f0641168cc6bbcc5742ba57d22eacf66..aa832f6223e1601cb71abe2461793fa9e32d08d8 100644 (file)
@@ -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),
     ],
 )
index f1d4043e8517655b962b3a4dbb9fde1f8b6dd2be..990d3ecaaaa582722199c44c5eac8133b4080afc 100644 (file)
@@ -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),
     ],
 )