From: Sebastián Ramírez Date: Sun, 22 Dec 2024 14:30:05 +0000 (+0000) Subject: 📝 Update markdown includes format (#1254) X-Git-Tag: 0.0.23~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5100200beab70261df745386fb86f77eb5eceed4;p=thirdparty%2Ffastapi%2Fsqlmodel.git 📝 Update markdown includes format (#1254) --- diff --git a/docs/advanced/decimal.md b/docs/advanced/decimal.md index 1234dcb8..ce971b20 100644 --- a/docs/advanced/decimal.md +++ b/docs/advanced/decimal.md @@ -33,45 +33,7 @@ For the database, **SQLModel** will use FastAPI uses the `response_model` to validate and **filter** the response data? In this case, we used `response_model=TeamPublic` and `response_model=HeroPublic`, so FastAPI will use them to filter the response data, even if we return a **table model** that includes **relationship attributes**: -//// tab | Python 3.10+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:102-107]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:156-161]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:104-109]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:158-163]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:104-109]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:158-163]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[102:107,156:161] hl[102,107,156,161] *} ## Don't Include All the Data @@ -304,69 +132,7 @@ Let's add the models `HeroPublicWithTeam` and `TeamPublicWithHeroes`. We'll add them **after** the other models so that we can easily reference the previous models. -//// tab | Python 3.10+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:59-64]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:61-66]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-4 7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:61-66]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[59:64] hl[59:60,63:64] *} These two models are very **simple in code**, but there's a lot happening here. Let's check it out. @@ -400,81 +166,7 @@ This will tell **FastAPI** to take the object that we return from the *path oper In the case of the hero, this tells FastAPI to extract the `team` too. And in the case of the team, to extract the list of `heroes` too. -//// tab | Python 3.10+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:111-116]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py[ln:165-170]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:113-118]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py[ln:167-172]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 8 12 17" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:113-118]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py[ln:167-172]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[111:116,165:170] hl[111,116,165,170] *} ## Check It Out in the Docs UI diff --git a/docs/tutorial/fastapi/response-model.md b/docs/tutorial/fastapi/response-model.md index b333d58b..f5c0ab9f 100644 --- a/docs/tutorial/fastapi/response-model.md +++ b/docs/tutorial/fastapi/response-model.md @@ -32,69 +32,7 @@ We can use `response_model` to tell FastAPI the schema of the data we want to se For example, we can pass the same `Hero` **SQLModel** class (because it is also a Pydantic model): -//// tab | Python 3.10+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py[ln:31-37]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py39.py[ln:33-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001.py[ln:33-39]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py ln[31:37] hl[31] *} ## List of Heroes in `response_model` @@ -102,73 +40,7 @@ We can also use other type annotations, the same way we can use with Pydantic fi First, we import `List` from `typing` and then we declare the `response_model` with `List[Hero]`: -//// tab | Python 3.10+ - -```Python hl_lines="3" - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py[ln:40-44]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py39.py[ln:42-46]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="1 5" -{!./docs_src/tutorial/fastapi/response_model/tutorial001.py[ln:1]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/response_model/tutorial001.py[ln:42-46]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/response_model/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py ln[40:44] hl[40] *} ## FastAPI and Response Model diff --git a/docs/tutorial/fastapi/session-with-dependency.md b/docs/tutorial/fastapi/session-with-dependency.md index e148452d..e81e9e67 100644 --- a/docs/tutorial/fastapi/session-with-dependency.md +++ b/docs/tutorial/fastapi/session-with-dependency.md @@ -6,69 +6,7 @@ Before we keep adding things, let's change a bit how we get the session for each Up to now, we have been creating a session in each *path operation*, in a `with` block. -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001_py310.py[ln:48-55]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001_py39.py[ln:50-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001.py[ln:50-57]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/delete/tutorial001_py310.py ln[48:55] hl[50] *} That's perfectly fine, but in many use cases we would want to use FastAPI Dependencies, for example to **verify** that the client is **logged in** and get the **current user** before executing any other code in the *path operation*. @@ -82,69 +20,7 @@ A **FastAPI** dependency is very simple, it's just a function that returns a val It could use `yield` instead of `return`, and in that case **FastAPI** will make sure it executes all the code **after** the `yield`, once it is done with the request. -//// tab | Python 3.10+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[40:42] *} ## Use the Dependency @@ -152,87 +28,7 @@ Now let's make FastAPI execute a dependency and get its value in the *path opera We import `Depends()` from `fastapi`. Then we use it in the *path operation function* in a **parameter**, the same way we declared parameters to get JSON bodies, path parameters, etc. -//// tab | Python 3.10+ - -```Python hl_lines="1 13" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 15" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 15" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[1,54] *} /// tip @@ -260,173 +56,13 @@ And because dependencies can use `yield`, FastAPI will make sure to run the code This means that in the main code of the *path operation function*, it will work equivalently to the previous version with the explicit `with` block. -//// tab | Python 3.10+ - -```Python hl_lines="14-18" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="16-20" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="16-20" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[55:59] *} In fact, you could think that all that block of code inside of the `create_hero()` function is still inside a `with` block for the **session**, because this is more or less what's happening behind the scenes. But now, the `with` block is not explicitly in the function, but in the dependency above: -//// tab | Python 3.10+ - -```Python hl_lines="7-8" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9-10" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-10" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[41:42] *} We will see how this is very useful when testing the code later. ✅ @@ -442,81 +78,7 @@ session: Session = Depends(get_session) And then we remove the previous `with` block with the old **session**. -//// tab | Python 3.10+ - -```Python hl_lines="13 24 33 42 57" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-104]!} -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="15 26 35 44 59" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-106]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="15 26 35 44 59" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-106]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:104] hl[54,65,74,83,98] *} ## Recap diff --git a/docs/tutorial/fastapi/simple-hero-api.md b/docs/tutorial/fastapi/simple-hero-api.md index fc85f2a0..c6f27f4b 100644 --- a/docs/tutorial/fastapi/simple-hero-api.md +++ b/docs/tutorial/fastapi/simple-hero-api.md @@ -32,54 +32,7 @@ We will start with the **simplest version**, with just heroes (no teams yet). This is almost the same code we have seen up to now in previous examples: -//// tab | Python 3.10+ - -```Python hl_lines="18-19" - -# One line of FastAPI imports here later 👈 -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:2]!} - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:5-20]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="20-21" -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:1]!} - -# One line of FastAPI imports here later 👈 -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:4]!} - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:7-22]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[2,5:20] hl[19:20] *} There's only one change here from the code we have used before, the `check_same_thread` in the `connect_args`. @@ -107,53 +60,7 @@ We will import the `FastAPI` class from `fastapi`. And then create an `app` object that is an instance of that `FastAPI` class: -//// tab | Python 3.10+ - -```Python hl_lines="1 6" -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:1-2]!} - -# SQLModel code here omitted 👈 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 8" -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:1-4]!} - -# SQLModel code here omitted 👈 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:25]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[1:2,23] hl[1,23] *} ## Create Database and Tables on `startup` @@ -161,49 +68,7 @@ We want to make sure that once the app starts running, the function `create_tabl This should be called only once at startup, not before every request, so we put it in the function to handle the `"startup"` event: -//// tab | Python 3.10+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-28]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:25-30]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:28] hl[26:28] *} ## Create Heroes *Path Operation* @@ -217,49 +82,7 @@ Let's create the **path operation** code to create a new hero. It will be called when a user sends a request with a `POST` **operation** to the `/heroes/` **path**: -//// tab | Python 3.10+ - -```Python hl_lines="11-12" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-37]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11-12" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:25-39]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:37] hl[31:32] *} /// info @@ -293,45 +116,7 @@ We will improve this further later, but for now, it already shows the power of h Now let's add another **path operation** to read all the heroes: -//// tab | Python 3.10+ - -```Python hl_lines="20-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-44]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="20-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:25-46]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:44] hl[40:44] *} This is pretty straightforward. diff --git a/docs/tutorial/fastapi/teams.md b/docs/tutorial/fastapi/teams.md index 9e366f2c..93d38829 100644 --- a/docs/tutorial/fastapi/teams.md +++ b/docs/tutorial/fastapi/teams.md @@ -18,63 +18,7 @@ Then we also inherit from the `TeamBase` for the `TeamCreate` and `TeamPublic` * And we also create a `TeamUpdate` **data model**. -//// tab | Python 3.10+ - -```Python hl_lines="5-7 10-13 16-17 20-21 24-26" -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:1-26]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="7-9 12-15 18-19 22-23 26-28" -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:1-28]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7-9 12-15 18-19 22-23 26-28" -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:1-28]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[1:26] hl[5:7,10:13,16:17,20:21,24:26] *} We now also have **relationship attributes**. 🎉 @@ -82,69 +26,7 @@ Let's now update the `Hero` models too. ## Update Hero Models -//// tab | Python 3.10+ - -```Python hl_lines="3-8 11-14 17-18 21-22 25-29" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:29-55]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-8 11-14 17-18 21-22 25-29" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:31-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-8 11-14 17-18 21-22 25-29" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:31-57]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[29:55] hl[29:34,37:40,43:44,47:48,51:55] *} We now have a `team_id` in the hero models. @@ -156,69 +38,7 @@ And even though the `HeroBase` is *not* a **table model**, we can declare `team_ Notice that the **relationship attributes**, the ones with `Relationship()`, are **only** in the **table models**, as those are the ones that are handled by **SQLModel** with SQLAlchemy and that can have the automatic fetching of data from the database when we access them. -//// tab | Python 3.10+ - -```Python hl_lines="11 38" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:5-55]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 38" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:7-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 38" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:7-57]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[5:55] hl[13,40] *} ## Path Operations for Teams @@ -226,69 +46,7 @@ Let's now add the **path operations** for teams. These are equivalent and very similar to the **path operations** for the **heroes** we had before, so we don't have to go over the details for each one, let's check the code. -//// tab | Python 3.10+ - -```Python hl_lines="3-9 12-20 23-28 31-47 50-57" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py[ln:136-190]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-9 12-20 23-28 31-47 50-57" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py[ln:138-192]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-9 12-20 23-28 31-47 50-57" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/teams/tutorial001.py[ln:138-192]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/teams/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[136:190] hl[136:142,145:153,156:161,164:180,183:190] *} ## Using Relationships Attributes diff --git a/docs/tutorial/fastapi/tests.md b/docs/tutorial/fastapi/tests.md index 922103bd..9c5255b7 100644 --- a/docs/tutorial/fastapi/tests.md +++ b/docs/tutorial/fastapi/tests.md @@ -14,13 +14,7 @@ We will use the application with the hero models, but without team models, and w Now we will see how useful it is to have this session dependency. ✨ -/// details | 👀 Full file preview - -```Python -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/main.py!} -``` - -/// +{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/main.py ln[0] *} ## File Structure @@ -304,21 +298,7 @@ But normally we will create **lots of other test functions**. And now all the bo Let's add some more tests: -```Python hl_lines="3 22" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py[ln:30-58]!} - -# Code below omitted 👇 -``` - -/// details | 👀 Full file preview - -```Python -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py!} -``` - -/// +{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[30:58] hl[30,49] *} /// tip @@ -338,23 +318,7 @@ For these examples, **that would have been simpler**, there's no need to separat But for the next test function, we will require **both fixtures**, the **client** and the **session**. -```Python hl_lines="6 10" -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py[ln:1-6]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py[ln:61-81]!} - -# Code below omitted 👇 -``` - -/// details | 👀 Full file preview - -```Python -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py!} -``` - -/// +{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[1:6,61:81] hl[6,61] *} In this test function, we want to check that the *path operation* to **read a list of heroes** actually sends us heroes. @@ -380,19 +344,7 @@ The function for the **client fixture** and the actual testing function will **b Using the same ideas, requiring the fixtures, creating data that we need for the tests, etc., we can now add the rest of the tests. They look quite similar to what we have done up to now. -```Python hl_lines="3 18 33" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py[ln:84-125]!} -``` - -/// details | 👀 Full file preview - -```Python -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py!} -``` - -/// +{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[84:125] hl[84,99,114] *} ## Run the Tests diff --git a/docs/tutorial/fastapi/update-extra-data.md b/docs/tutorial/fastapi/update-extra-data.md index 6bbd72ad..95843144 100644 --- a/docs/tutorial/fastapi/update-extra-data.md +++ b/docs/tutorial/fastapi/update-extra-data.md @@ -38,69 +38,7 @@ The `Hero` table model will now store a new field `hashed_password`. And the data models for `HeroCreate` and `HeroUpdate` will also have a new field `password` that will contain the plain text password sent by clients. -//// tab | Python 3.10+ - -```Python hl_lines="11 15 26" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:5-28]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 15 26" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:7-30]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 15 26" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:7-30]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[5:28] hl[13,17,28] *} When a client is creating a new hero, they will send the `password` in the request body. @@ -112,81 +50,7 @@ The app will receive the data from the client using the `HeroCreate` model. This contains the `password` field with the plain text password, and we cannot use that one. So we need to generate a hash from it. -//// tab | Python 3.10+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:55-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:44-46]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:57-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:44-46]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:57-59]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[42:44,55:57] hl[57] *} ## Create an Object with Extra Data @@ -242,69 +106,7 @@ So now, `db_user_dict` has the updated `age` field with `32` instead of `None` a Similar to how dictionaries have an `update` method, **SQLModel** models have a parameter `update` in `Hero.model_validate()` that takes a dictionary with extra data, or data that should take precedence: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:55-64]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:57-66]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:57-66]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[55:64] hl[60] *} Now, `db_hero` (which is a *table model* `Hero`) will extract its values from `hero` (which is a *data model* `HeroCreate`), and then it will **`update`** its values with the extra data from the dictionary `extra_data`. @@ -318,69 +120,7 @@ Now let's say we want to **update a hero** that already exists in the database. The same way as before, to avoid removing existing data, we will use `exclude_unset=True` when calling `hero.model_dump()`, to get a dictionary with only the data sent by the client. -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:83-89]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:85-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:85-91]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[83:89] hl[89] *} Now, this `hero_data` dictionary could contain a `password`. We need to check it, and if it's there, we need to generate the `hashed_password`. @@ -390,69 +130,7 @@ And then we can update the `db_hero` object using the method `db_hero.sqlmodel_u It takes a model object or dictionary with the data to update the object and also an **additional `update` argument** with extra data. -//// tab | Python 3.10+ - -```Python hl_lines="15" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py[ln:83-99]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="15" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py[ln:85-101]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="15" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial002.py[ln:85-101]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[83:99] hl[95] *} /// tip diff --git a/docs/tutorial/fastapi/update.md b/docs/tutorial/fastapi/update.md index 6b2411be..0aed115d 100644 --- a/docs/tutorial/fastapi/update.md +++ b/docs/tutorial/fastapi/update.md @@ -22,69 +22,7 @@ Because each field is **actually different** (we just change it to `Optional`, b So, let's create this new `HeroUpdate` model: -//// tab | Python 3.10+ - -```Python hl_lines="21-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py[ln:5-26]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="21-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py[ln:7-28]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="21-24" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001.py[ln:7-28]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[5:26] hl[23:26] *} This is almost the same as `HeroBase`, but all the fields are optional, so we can't simply inherit from `HeroBase`. @@ -94,69 +32,7 @@ Now let's use this model in the *path operation* to update a hero. We will use a `PATCH` HTTP operation. This is used to **partially update data**, which is what we are doing. -//// tab | Python 3.10+ - -```Python hl_lines="3-4" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py[ln:74-89]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-4" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-4" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[74:75] *} We also read the `hero_id` from the *path parameter* and the request body, a `HeroUpdate`. @@ -166,69 +42,7 @@ We take a `hero_id` with the **ID** of the hero **we want to update**. So, we need to read the hero from the database, with the **same logic** we used to **read a single hero**, checking if it exists, possibly raising an error for the client if it doesn't exist, etc. -//// tab | Python 3.10+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py[ln:74-89]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[77:79] *} ### Get the New Data @@ -280,69 +94,7 @@ Then the dictionary we would get in Python using `hero.model_dump(exclude_unset= Then we use that to get the data that was actually sent by the client: -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py[ln:74-89]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[80] *} /// tip Before SQLModel 0.0.14, the method was called `hero.dict(exclude_unset=True)`, but it was renamed to `hero.model_dump(exclude_unset=True)` to be consistent with Pydantic v2. @@ -352,69 +104,7 @@ Before SQLModel 0.0.14, the method was called `hero.dict(exclude_unset=True)`, b Now that we have a **dictionary with the data sent by the client**, we can use the method `db_hero.sqlmodel_update()` to update the object `db_hero`. -//// tab | Python 3.10+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py[ln:74-89]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/update/tutorial001.py[ln:76-91]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/update/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[81] *} /// tip diff --git a/docs/tutorial/indexes.md b/docs/tutorial/indexes.md index fea4289a..80314ef2 100644 --- a/docs/tutorial/indexes.md +++ b/docs/tutorial/indexes.md @@ -20,25 +20,7 @@ Are you already a **SQL expert** and don't have time for all my explanations? Fine, in that case, you can **sneak peek** the final code to create indexes here. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[0] *} ..but if you are not an expert, **continue reading**, this will probably be useful. 🤓 @@ -275,87 +257,11 @@ The change in code is underwhelming, it's very simple. 😆 Here's the `Hero` model we had before: -//// tab | Python 3.10+ - -```Python hl_lines="6" -{!./docs_src/tutorial/where/tutorial001_py310.py[ln:1-8]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -{!./docs_src/tutorial/where/tutorial001.py[ln:1-10]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial001_py310.py ln[1:8] hl[6] *} Let's now update it to tell **SQLModel** to create an index for the `name` field when creating the table: -//// tab | Python 3.10+ - -```Python hl_lines="6" -{!./docs_src/tutorial/indexes/tutorial001_py310.py[ln:1-8]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -{!./docs_src/tutorial/indexes/tutorial001.py[ln:1-10]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial001_py310.py ln[1:8] hl[6] *} We use the same `Field()` again as we did before, and set `index=True`. That's it! 🚀 @@ -377,49 +283,7 @@ The SQL database will figure it out **automatically**. ✨ This is great because it means that indexes are very **simple to use**. But it might also feel counterintuitive at first, as you are **not doing anything** explicitly in the code to make it obvious that the index is useful, it all happens in the database behind the scenes. -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/indexes/tutorial001_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/indexes/tutorial001.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial001_py310.py ln[34:39] hl[36] *} This is exactly the same code as we had before, but now the database will **use the index** underneath. @@ -462,45 +326,7 @@ secret_name='Dive Wilson' age=None id=1 name='Deadpond' We are going to query the `hero` table doing comparisons on the `age` field too, so we should **define an index** for that one as well: -//// tab | Python 3.10+ - -```Python hl_lines="8" -{!./docs_src/tutorial/indexes/tutorial002_py310.py[ln:1-8]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10" -{!./docs_src/tutorial/indexes/tutorial002.py[ln:1-10]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[1:8] hl[8] *} In this case, we want the default value of `age` to continue being `None`, so we set `default=None` when using `Field()`. diff --git a/docs/tutorial/insert.md b/docs/tutorial/insert.md index 9439cc37..272287e6 100644 --- a/docs/tutorial/insert.md +++ b/docs/tutorial/insert.md @@ -145,49 +145,7 @@ So, the first step is to simply create an instance of `Hero`. We'll create 3 right away, for the 3 heroes: -//// tab | Python 3.10+ - -```Python -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial002_py310.py[ln:21-24]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial002.py[ln:23-26]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[21:24] *} /// tip @@ -219,91 +177,11 @@ We would re-use the same **engine** in all the code, everywhere in the applicati The first step is to import the `Session` class: -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/insert/tutorial001_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/insert/tutorial001.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[1] hl[1] *} Then we can create a new session: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial001_py310.py[ln:21-26]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial001.py[ln:23-28]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:26] hl[26] *} The new `Session` takes an `engine` as a parameter. And it will use the **engine** underneath. @@ -317,47 +195,7 @@ We will see a better way to create a **session** using a `with` block later. Now that we have some hero model instances (some objects in memory) and a **session**, the next step is to add them to the session: -//// tab | Python 3.10+ - -```Python hl_lines="9-11" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial001_py310.py[ln:21-30]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-11" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial001.py[ln:23-32]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:30] hl[28:30] *} By this point, our heroes are *not* stored in the database yet. @@ -381,47 +219,7 @@ This ensures that the data is saved in a single batch, and that it will all succ Now that we have the heroes in the **session** and that we are ready to save all that to the database, we can **commit** the changes: -//// tab | Python 3.10+ - -```Python hl_lines="13" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial001_py310.py[ln:21-32]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial001.py[ln:23-34]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:32] hl[32] *} Once this line is executed, the **session** will use the **engine** to save all the data in the database by sending the corresponding SQL. @@ -448,87 +246,11 @@ if __name__ == "__main__": But to keep things a bit more organized, let's instead create a new function `main()` that will contain all the code that should be executed when called as an independent script, and we can put there the previous function `create_db_and_tables()`, and add the new function `create_heroes()`: -//// tab | Python 3.10+ - -```Python hl_lines="2 4" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002_py310.py[ln:34-36]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="2 4" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002.py[ln:36-38]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[34:36] hl[34,36] *} And then we can call that single `main()` function from that main block: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002_py310.py[ln:34-40]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002.py[ln:36-42]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[34:40] hl[40] *} By having everything that should happen when called as a script in a single function, we can easily add more code later on. @@ -583,49 +305,7 @@ The **session** holds some resources, like connections from the engine. So once we are done with the session, we should **close** it to make it release those resources and finish its cleanup: -//// tab | Python 3.10+ - -```Python hl_lines="16" -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial001_py310.py[ln:21-34]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="16" -# Code above omitted 👆 - -{!./docs_src/tutorial/insert/tutorial001.py[ln:23-36]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:34] hl[34] *} But what happens if we forget to close the session? @@ -639,43 +319,7 @@ It's good to know how the `Session` works and how to create and close it manuall But there's a better way to handle the session, using a `with` block: -//// tab | Python 3.10+ - -```Python hl_lines="7-12" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002_py310.py[ln:21-31]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7-12" -# Code above omitted 👆 -{!./docs_src/tutorial/insert/tutorial002.py[ln:23-33]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[21:31] hl[26:31] *} This is the same as creating the session manually and then manually closing it. But here, using a `with` block, it will be automatically created when **starting** the `with` block and assigned to the variable `session`, and it will be automatically closed after the `with` block is **finished**. diff --git a/docs/tutorial/limit-and-offset.md b/docs/tutorial/limit-and-offset.md index aa0d659a..18e8aa14 100644 --- a/docs/tutorial/limit-and-offset.md +++ b/docs/tutorial/limit-and-offset.md @@ -14,97 +14,13 @@ We will continue with the same code as before, but we'll modify it a little the Again, we will create several heroes to have some data to select from: -//// tab | Python 3.10+ - -```Python hl_lines="4-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial001_py310.py[ln:21-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="4-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial001.py[ln:23-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/offset_and_limit/tutorial001_py310.py ln[21:39] hl[22:28] *} ## Review Select All This is the code we had to select all the heroes in the `select()` examples: -//// tab | Python 3.10+ - -```Python hl_lines="3-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial003_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial003.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial003_py310.py ln[34:39] hl[34:39] *} But this would get us **all** the heroes at the same time, in a database that could have thousands, that could be problematic. @@ -112,49 +28,7 @@ But this would get us **all** the heroes at the same time, in a database that co We currently have 7 heroes in the database. But we could as well have thousands, so let's limit the results to get only the first 3: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial001_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial001.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/offset_and_limit/tutorial001_py310.py ln[42:47] hl[44] *} The special **select** object we get from `select()` also has a method `.limit()` that we can use to limit the results to a certain number. @@ -217,49 +91,7 @@ How do we get the next 3? We can use `.offset()`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial002_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial002.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/offset_and_limit/tutorial002_py310.py ln[42:47] hl[44] *} The way this works is that the special **select** object we get from `select()` has methods like `.where()`, `.offset()` and `.limit()`. @@ -298,49 +130,7 @@ INFO Engine [no key 0.00020s] (3, 3) Then to get the next batch of 3 rows we would offset all the ones we already saw, the first 6: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial003_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial003.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/offset_and_limit/tutorial003_py310.py ln[42:47] hl[44] *} The database right now has **only 7 rows**, so this query can only get 1 row. @@ -395,49 +185,7 @@ If you try that in **DB Browser for SQLite**, you will get the same result: Of course, you can also combine `.limit()` and `.offset()` with `.where()` and other methods you will learn about later: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial004_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/offset_and_limit/tutorial004.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/offset_and_limit/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/offset_and_limit/tutorial004_py310.py ln[42:47] hl[44] *} ## Run the Program with Limit, Offset, and Where on the Command Line diff --git a/docs/tutorial/many-to-many/create-data.md b/docs/tutorial/many-to-many/create-data.md index bcd87629..d53a64c5 100644 --- a/docs/tutorial/many-to-many/create-data.md +++ b/docs/tutorial/many-to-many/create-data.md @@ -8,101 +8,13 @@ We'll create data for this same **many-to-many** relationship with a link table: We'll continue from where we left off with the previous code. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[0] *} ## Create Heroes As we have done before, we'll create a function `create_heroes()` and we'll create some teams and heroes in it: -//// tab | Python 3.10+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:36-54]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:42-60]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:42-60]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[36:54] hl[44] *} This is very similar to what we have done before. @@ -116,137 +28,13 @@ See how **Deadpond** now belongs to the two teams? Now let's do as we have done before, `commit` the **session**, `refresh` the data, and print it: -//// tab | Python 3.10+ - -```Python hl_lines="22-25 27-29 31-36" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:36-69]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="22-25 27-29 31-36" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:42-75]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="22-25 27-29 31-36" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:42-75]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[36:69] hl[55:58,60:62,64:69] *} ## Add to Main As before, add the `create_heroes()` function to the `main()` function to make sure it is called when running this program from the command line: -//// tab | Python 3.10+ - -```Python -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:72-74]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:78-80]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:78-80]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[72:74] *} ## Run the Program diff --git a/docs/tutorial/many-to-many/create-models-with-link.md b/docs/tutorial/many-to-many/create-models-with-link.md index 308c678c..69d0f968 100644 --- a/docs/tutorial/many-to-many/create-models-with-link.md +++ b/docs/tutorial/many-to-many/create-models-with-link.md @@ -12,63 +12,7 @@ As we want to support a **many-to-many** relationship, now we need a **link tabl We can create it just as any other **SQLModel**: -//// tab | Python 3.10+ - -```Python hl_lines="4-6" -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:1-6]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="6-12" -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:1-12]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6-12" -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:1-12]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[1:6] hl[4:6] *} This is a **SQLModel** class model table like any other. @@ -82,69 +26,7 @@ And **both fields are primary keys**. We hadn't used this before. 🤓 Let's see the `Team` model, it's almost identical as before, but with a little change: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:9-14]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:15-20]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:15-20]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[9:14] hl[14] *} The **relationship attribute `heroes`** is still a list of heroes, annotated as `List["Hero"]`. Again, we use `"Hero"` in quotes because we haven't declared that class yet by this point in the code (but as you know, editors and **SQLModel** understand that). @@ -158,69 +40,7 @@ And here's the important part to allow the **many-to-many** relationship, we use Let's see the other side, here's the `Hero` model: -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:17-23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:23-29]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:23-29]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[17:23] hl[23] *} We **removed** the previous `team_id` field (column) because now the relationship is done via the link table. 🔥 @@ -238,140 +58,11 @@ And now we have a **`link_model=HeroTeamLink`**. ✨ The same as before, we will have the rest of the code to create the **engine**, and a function to create all the tables `create_db_and_tables()`. -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:26-33]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:32-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:32-39]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// - +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[26:33] hl[32] *} And as in previous examples, we will add that function to a function `main()`, and we will call that `main()` function in the main block: -//// tab | Python 3.10+ - -```Python hl_lines="4" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:72-73]!} - # We will do more stuff here later 👈 - -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py[ln:77-78]!} -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="4" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:78-79]!} - # We will do more stuff here later 👈 - -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py[ln:83-84]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="4" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:78-79]!} - # We will do more stuff here later 👈 - -{!./docs_src/tutorial/many_to_many/tutorial001.py[ln:83-84]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// - +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[72:73,77:78] hl[73] *} ## Run the Code diff --git a/docs/tutorial/many-to-many/link-with-extra-fields.md b/docs/tutorial/many-to-many/link-with-extra-fields.md index 653ef223..7c7756bc 100644 --- a/docs/tutorial/many-to-many/link-with-extra-fields.md +++ b/docs/tutorial/many-to-many/link-with-extra-fields.md @@ -32,69 +32,7 @@ We will add a new field `is_training`. And we will also add two **relationship attributes**, for the linked `team` and `hero`: -//// tab | Python 3.10+ - -```Python hl_lines="6 8-9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:4-10]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="10 12-13" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:6-16]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10 12-13" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:6-16]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[4:10] hl[7,9:10] *} The new **relationship attributes** have their own `back_populates` pointing to new relationship attributes we will create in the `Hero` and `Team` models: @@ -115,69 +53,7 @@ Now let's update the `Team` model. We no longer have the `heroes` relationship attribute, and instead we have the new `hero_links` attribute: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:13-18]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:19-24]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:19-24]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[13:18] hl[18] *} ## Update Hero Model @@ -185,69 +61,7 @@ The same with the `Hero` model. We change the `teams` relationship attribute for `team_links`: -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:21-27]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:27-33]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:27-33]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[21:27] hl[27] *} ## Create Relationships @@ -255,69 +69,7 @@ Now the process to create relationships is very similar. But now we create the **explicit link models** manually, pointing to their hero and team instances, and specifying the additional link data (`is_training`): -//// tab | Python 3.10+ - -```Python hl_lines="21-30 32-35" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:40-79]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="21-30 32-35" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:46-85]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="21-30 32-35" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:46-85]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[40:79] hl[58:67,69:72] *} We are just adding the link model instances to the session, because the link model instances are connected to the heroes and teams, they will be also automatically included in the session when we commit. @@ -415,69 +167,7 @@ Now, to add a new relationship, we have to create a new `HeroTeamLink` instance Here we do that in the `update_heroes()` function: -//// tab | Python 3.10+ - -```Python hl_lines="10-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:82-97]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="10-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:88-103]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:88-103]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[82:97] hl[89:94] *} ## Run the Program with the New Relationship @@ -558,81 +248,7 @@ So now we want to update the status of `is_training` to `False`. We can do that by iterating on the links: -//// tab | Python 3.10+ - -```Python hl_lines="8-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:82-83]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py[ln:99-107]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:88-89]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py[ln:105-113]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8-10" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:88-89]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial003.py[ln:105-113]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[82:83,99:107] hl[99:101] *} ## Run the Program with the Updated Relationships diff --git a/docs/tutorial/many-to-many/update-remove-relationships.md b/docs/tutorial/many-to-many/update-remove-relationships.md index 555289a0..ebc9ba3a 100644 --- a/docs/tutorial/many-to-many/update-remove-relationships.md +++ b/docs/tutorial/many-to-many/update-remove-relationships.md @@ -4,33 +4,7 @@ Now we'll see how to update and remove these **many-to-many** relationships. We'll continue from where we left off with the previous code. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[0] *} ## Get Data to Update @@ -42,135 +16,11 @@ As you already know how these goes, I'll use the **short version** and get the d And because we are now using `select()`, we also have to import it. -//// tab | Python 3.10+ - -```Python hl_lines="1 5-10" -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:1]!} - -# Some code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-77]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 7-12" -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:1-3]!} - -# Some code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-83]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 7-12" -{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:1-3]!} - -# Some code here omitted 👈 - -{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:78-83]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[1,72:77] hl[1,72:77] *} And of course, we have to add `update_heroes()` to our `main()` function: -//// tab | Python 3.10+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:94-101]!} -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:100-107]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:100-107]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[94:101] hl[97] *} ## Add Many-to-Many Relationships @@ -178,69 +28,7 @@ Now let's imagine that **Spider-Boy** thinks that the **Z-Force** team is super We can use the same **relationship attributes** to include `hero_spider_boy` in the `team_z_force.heroes`. -//// tab | Python 3.10+ - -```Python hl_lines="10-12 14-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-84]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="10-12 14-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-90]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10-12 14-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:78-90]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[72:84] hl[79:81,83:84] *} /// tip @@ -325,69 +113,7 @@ Because `hero_spider_boy.teams` is just a list (a special list managed by SQLAlc In this case, we use the method `.remove()`, that takes an item and removes it from the list. -//// tab | Python 3.10+ - -```Python hl_lines="17-19 21-22" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py[ln:72-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="17-19 21-22" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py[ln:78-97]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="17-19 21-22" -# Code above omitted 👆 - -{!./docs_src/tutorial/many_to_many/tutorial002.py[ln:78-97]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/many_to_many/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[72:91] hl[86:88,90:91] *} And this time, just to show again that by using `back_populates` **SQLModel** (actually SQLAlchemy) takes care of connecting the models by their relationships, even though we performed the operation from the `hero_spider_boy` object (modifying `hero_spider_boy.teams`), we are adding `team_z_force` to the **session**. And we commit that, without even add `hero_spider_boy`. diff --git a/docs/tutorial/one.md b/docs/tutorial/one.md index 4e770dbc..54cdbbea 100644 --- a/docs/tutorial/one.md +++ b/docs/tutorial/one.md @@ -14,25 +14,7 @@ Let's see the utilities to read a single row. We'll continue with the same examples we have been using in the previous chapters to create and select data and we'll keep updating them. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[0] *} If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results. @@ -40,97 +22,13 @@ If you already executed the previous examples and have a database with data, **r We have been iterating over the rows in a `result` object like: -//// tab | Python 3.10+ - -```Python hl_lines="7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/indexes/tutorial002_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/indexes/tutorial002.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/indexes/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[42:47] hl[46:47] *} But let's say that we are not interested in all the rows, just the **first** one. We can call the `.first()` method on the `results` object to get the first row: -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial001_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial001.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial001_py310.py ln[42:47] hl[46] *} This will return the first object in the `results` (if there was any). @@ -171,49 +69,7 @@ It would be possible that the SQL query doesn't find any row. In that case, `.first()` will return `None`: -//// tab | Python 3.10+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial002_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial002.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial002_py310.py ln[42:47] hl[44,46] *} In this case, as there's no hero with an age less than 25, `.first()` will return `None`. @@ -246,49 +102,7 @@ And if there was more than one, it would mean that there's an error in the syste In that case, instead of `.first()` we can use `.one()`: -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial003_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial003.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial003_py310.py ln[42:47] hl[46] *} Here we know that there's only one `"Deadpond"`, and there shouldn't be any more than one. @@ -344,49 +158,7 @@ sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when exactly one w Of course, even if we don't duplicate the data, we could get the same error if we send a query that finds more than one row and expect exactly one with `.one()`: -//// tab | Python 3.10+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial004_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial004.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial004_py310.py ln[42:47] hl[44,46] *} That would find 2 rows, and would end up with the same error. @@ -394,49 +166,7 @@ That would find 2 rows, and would end up with the same error. And also, if we get no rows at all with `.one()`, it will also raise an error: -//// tab | Python 3.10+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial005_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial005.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial005_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial005.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial005_py310.py ln[42:47] hl[44,46] *} In this case, as there are no heroes with an age less than 25, `.one()` will raise an error. @@ -469,49 +199,7 @@ sqlalchemy.exc.NoResultFound: No row was found when one was required Of course, with `.first()` and `.one()` you would also probably write all that in a more compact form most of the time, all in a single line (or at least a single Python statement): -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial006_py310.py[ln:42-45]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial006.py[ln:44-47]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial006_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial006.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial006_py310.py ln[42:45] hl[44] *} That would result in the same as some examples above. @@ -521,49 +209,7 @@ In many cases you might want to select a single row by its Id column with the ** You could do it the same way we have been doing with a `.where()` and then getting the first item with `.first()`: -//// tab | Python 3.10+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial007_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5 7" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial007.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial007_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial007.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial007_py310.py ln[42:47] hl[44,46] *} That would work correctly, as expected. But there's a shorter version. 👇 @@ -571,49 +217,7 @@ That would work correctly, as expected. But there's a shorter version. 👇 As selecting a single row by its Id column with the **primary key** is a common operation, there's a shortcut for it: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial008_py310.py[ln:42-45]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial008.py[ln:44-47]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial008_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial008.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial008_py310.py ln[42:45] hl[44] *} `session.get(Hero, 1)` is an equivalent to creating a `select()`, then filtering by Id using `.where()`, and then getting the first item with `.first()`. @@ -642,49 +246,7 @@ Hero: secret_name='Dive Wilson' age=None id=1 name='Deadpond' `.get()` behaves similar to `.first()`, if there's no data it will simply return `None` (instead of raising an error): -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial009_py310.py[ln:42-45]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/one/tutorial009.py[ln:44-47]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/one/tutorial009_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/one/tutorial009.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/one/tutorial009_py310.py ln[42:45] hl[44] *} Running that will output: diff --git a/docs/tutorial/relationship-attributes/back-populates.md b/docs/tutorial/relationship-attributes/back-populates.md index 0eaa7bb6..7cb9de6c 100644 --- a/docs/tutorial/relationship-attributes/back-populates.md +++ b/docs/tutorial/relationship-attributes/back-populates.md @@ -20,63 +20,7 @@ Let's understand that better with an example. Let's see how that works by writing an **incomplete** version first, without `back_populates`: -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[1:19] hl[9,19] *} ## Read Data Objects @@ -84,69 +28,7 @@ Now, we will get the **Spider-Boy** hero and, *independently*, the **Preventers* As you already know how this works, I won't separate that in a select `statement`, `results`, etc. Let's use the shorter form in a single call: -//// tab | Python 3.10+ - -```Python hl_lines="5-7 9-11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-111]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="5-7 9-11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-113]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5-7 9-11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-113]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:111] hl[105:107,109:111] *} /// tip @@ -158,69 +40,7 @@ When writing your own code, this is probably the style you will use most often, Now, let's print the current **Spider-Boy**, the current **Preventers** team, and particularly, the current **Preventers** list of heroes: -//// tab | Python 3.10+ - -```Python hl_lines="13-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-115]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="13-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-117]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13-15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-117]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:115] hl[113:115] *} Up to this point, it's all good. 😊 @@ -242,81 +62,7 @@ Notice that we have **Spider-Boy** there. Now let's update **Spider-Boy**, removing him from the team by setting `hero_spider_boy.team = None` and then let's print this object again: -//// tab | Python 3.10+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:117-121]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,117:121] hl[117,121] *} The first important thing is, we *haven't committed* the hero yet, so accessing the list of heroes would not trigger an automatic refresh. @@ -356,81 +102,7 @@ Oh, no! 😱 **Spider-Boy** is still listed there! Now, if we commit it and print again: -//// tab | Python 3.10+ - -```Python hl_lines="8-9 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:123-130]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8-9 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:125-132]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8-9 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:125-132]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,123:130] hl[123:124,130] *} When we access `preventers_team.heroes` after the `commit`, that triggers a refresh, so we get the latest list, without **Spider-Boy**, so that's fine again: @@ -462,141 +134,11 @@ That's what `back_populates` is for. ✨ Let's add it back: -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} And we can keep the rest of the code the same: -//// tab | Python 3.10+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:103-104]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:117-121]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8 12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:105-106]!} - - # Code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[103:104,117:121] hl[117,121] *} /// tip @@ -629,63 +171,7 @@ Now that you know why `back_populates` is there, let's review the exact value ag It's quite simple code, it's just a string, but it might be confusing to think exactly *what* string should go there: -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} The string in `back_populates` is the name of the attribute *in the other* model, that will reference *the current* model. @@ -693,69 +179,7 @@ The string in `back_populates` is the name of the attribute *in the other* model So, in the class `Team`, we have an attribute `heroes` and we declare it with `Relationship(back_populates="team")`. -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:4-9]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:6-11]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:6-11]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[4:9] hl[9] *} The string in `back_populates="team"` refers to the attribute `team` in the class `Hero` (the other class). @@ -763,69 +187,7 @@ And, in the class `Hero`, we declare an attribute `team`, and we declare it with So, the string `"heroes"` refers to the attribute `heroes` in the class `Team`. -//// tab | Python 3.10+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:12-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:14-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:14-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[12:19] hl[19] *} /// tip @@ -850,66 +212,4 @@ So, `back_populates` would most probably be something like `"hero"` or `"heroes" -//// tab | Python 3.10+ - -```Python hl_lines="3 10 13 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py[ln:27-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 10 13 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py[ln:29-41]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 10 13 15" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py[ln:29-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py ln[27:39] hl[27,34,37,39] *} diff --git a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md b/docs/tutorial/relationship-attributes/cascade-delete-relationships.md index 1604b978..1683c9cb 100644 --- a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md +++ b/docs/tutorial/relationship-attributes/cascade-delete-relationships.md @@ -109,63 +109,7 @@ When creating a `Relationship()`, we can set `cascade_delete=True`. This configures SQLModel to **automatically delete** the related records (heroes) **when the initial one is deleted** (a team). -//// tab | Python 3.10+ - -```Python hl_lines="9" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:1-9]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:1-11]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:1-11]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:9] hl[9] *} With this configuration, when we delete a team, SQLModel (actually SQLAlchemy) will: @@ -211,63 +155,7 @@ The `ondelete` parameter will set a SQL `ON DELETE` in the **foreign key column* If we want to configure the database to **automatically delete** the related records when the parent is deleted, we can set `ondelete="CASCADE"`. -//// tab | Python 3.10+ - -```Python hl_lines="18" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:19] hl[18] *} Now, when we **create the tables** in the database, the `team_id` column in the `Hero` table will have an `ON DELETE CASCADE` in its definition at the database level. @@ -331,69 +219,7 @@ class Team(SQLModel, table=True): Now, when we **delete a team**, we don't need to do anything else, it's **automatically** going to **delete its heroes**. -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:76-82]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:80-86]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:80-86]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[76:82] hl[80] *} ## Confirm Heroes are Deleted @@ -401,69 +227,7 @@ We can confirm that **after deleting the team** `Wakaland`, the heroes `Black Li If we try to select them from the database, we will **no longer find them**. -//// tab | Python 3.10+ - -```Python hl_lines="5 8 10 13" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:85-95]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="5 8 10 13" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:89-99]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5 8 10 13" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:89-99]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[85:95] hl[87,90,92,95] *} ## Run the Program with `cascade_delete=True` and `ondelete="CASCADE"` @@ -533,64 +297,7 @@ We can configure the database to **set the foreign key** (the `team_id` in the ` In this case, the side with `Relationship()` won't have `cascade_delete`, but the side with `Field()` and a `foreign_key` will have `ondelete="SET NULL"`. -//// tab | Python 3.10+ - -```Python hl_lines="19" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py[ln:1-23]!} - -# Code below omitted 👇 -``` - - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[1:21] hl[19] *} The configuration above is setting the `team_id` column from the `Hero` table to have an `ON DELETE SET NULL`. @@ -626,69 +333,7 @@ But if you delete a team from code, by default, SQLModel (actually SQLAlchemy) w Removing a team has the **same code** as before, the only thing that changes is the configuration underneath in the database. -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py[ln:78-84]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py[ln:80-86]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py[ln:80-86]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[78:84] hl[82] *} The result would be these tables. @@ -793,69 +438,7 @@ If you know your database would be able to correctly handle the deletes or updat To be able to test this out with SQLite, we first need to enable foreign key support. -//// tab | Python 3.10+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py[ln:30-33]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py[ln:32-35]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py[ln:32-35]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[30:33] hl[33] *} /// info @@ -869,63 +452,7 @@ Now let's update the table model for `Team` to use `passive_deletes="all"` in th We will also use `ondelete="SET NULL"` in the `Hero` model table, in the foreign key `Field()` for the `team_id` to make the database set those fields to `NULL` automatically. -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[1:21] hl[9,19] *} ### Run the Program with `passive_deletes` @@ -989,69 +516,7 @@ For SQLite, this also needs enabling foreign key support. As `ondelete="RESTRICT"` is mainly a database-level constraint, let's enable foreign key support in SQLite first to be able to test it. -//// tab | Python 3.10+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py[ln:30-33]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py[ln:32-35]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py[ln:32-35]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[30:33] hl[33] *} ### Use `ondelete="RESTRICT"` @@ -1065,63 +530,7 @@ Notice that we don't set `cascade_delete` in the `Team` model table. /// -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py[ln:1-23]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[1:21] hl[9,19] *} ### Run the Program with `RESTRICT`, See the Error @@ -1184,69 +593,7 @@ Calling `team.heroes.clear()` is very similar to what SQLModel (actually SQLAlch /// -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py[ln:80-88]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py[ln:82-90]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py[ln:82-90]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py ln[80:88] hl[84] *} ### Run the Program Deleting Heroes First diff --git a/docs/tutorial/relationship-attributes/create-and-update-relationships.md b/docs/tutorial/relationship-attributes/create-and-update-relationships.md index ce2f4d87..d0de5673 100644 --- a/docs/tutorial/relationship-attributes/create-and-update-relationships.md +++ b/docs/tutorial/relationship-attributes/create-and-update-relationships.md @@ -6,49 +6,7 @@ Let's see now how to create data with relationships using these new **relationsh Let's check the old code we used to create some heroes and teams: -//// tab | Python 3.10+ - -```Python hl_lines="9 12 18 24" -# Code above omitted 👆 - -{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-58]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9 12 18 24" -# Code above omitted 👆 - -{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:31-60]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/connect/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:58] hl[35,38,44,50] *} There are several things to **notice** here. @@ -68,69 +26,7 @@ This is the first area where these **relationship attributes** can help. 🤓 Now let's do all that, but this time using the new, shiny `Relationship` attributes: -//// tab | Python 3.10+ - -```Python hl_lines="9 12 18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:32-55]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9 12 18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:34-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9 12 18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:34-57]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[32:55] hl[38,41,47] *} Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`. @@ -146,81 +42,7 @@ And then, as you can see, we only have to do one `commit()`. The same way we could assign an integer with a `team.id` to a `hero.team_id`, we can also assign the `Team` instance to the `hero.team`: -//// tab | Python 3.10+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:57-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:59-63]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="8" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:59-63]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,57:61] hl[57] *} ## Create a Team with Heroes @@ -228,81 +50,7 @@ Before, we created some `Team` instances and passed them in the `team=` argument We could also create the `Hero` instances first, and then pass them in the `heroes=` argument that takes a list, when creating a `Team` instance: -//// tab | Python 3.10+ - -```Python hl_lines="13 15-16" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:63-73]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="13 15-16" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:65-75]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13 15-16" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:65-75]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,63:73] hl[68,70:71] *} Here we create two heroes first, **Black Lion** and **Princess Sure-E**, and then we pass them in the `heroes` argument. @@ -318,81 +66,7 @@ As the attribute `team.heroes` behaves like a list, we can simply append to it. Let's create some more heroes and add them to the `team_preventers.heroes` list attribute: -//// tab | Python 3.10+ - -```Python hl_lines="14-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:75-91]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="14-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:77-93]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="14-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!} - - # Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:77-93]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,75:91] hl[81:85] *} The attribute `team_preventers.heroes` behaves like a list. But it's a special type of list, because when we modify it adding heroes to it, **SQLModel** (actually SQLAlchemy) **keeps track of the necessary changes** to be done in the database. diff --git a/docs/tutorial/relationship-attributes/define-relationships-attributes.md b/docs/tutorial/relationship-attributes/define-relationships-attributes.md index adbcc7df..78398919 100644 --- a/docs/tutorial/relationship-attributes/define-relationships-attributes.md +++ b/docs/tutorial/relationship-attributes/define-relationships-attributes.md @@ -41,45 +41,7 @@ Now that you know how these tables work underneath and how the model classes rep Up to now, we have only used the `team_id` column to connect the tables when querying with `select()`: -//// tab | Python 3.10+ - -```Python hl_lines="16" -{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:1-16]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="18" -{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:1-18]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/connect/insert/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[1:16] hl[16] *} This is a **plain field** like all the others, all representing a **column in the table**. @@ -87,123 +49,11 @@ But now let's add a couple of new special attributes to these model classes, let First, import `Relationship` from `sqlmodel`: -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1] hl[1] *} Next, use that `Relationship` to declare a new attribute in the model classes: -//// tab | Python 3.10+ - -```Python hl_lines="9 19" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11 21" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9,19] *} ## What Are These Relationship Attributes diff --git a/docs/tutorial/relationship-attributes/read-relationships.md b/docs/tutorial/relationship-attributes/read-relationships.md index fe9e5ae0..a59ec835 100644 --- a/docs/tutorial/relationship-attributes/read-relationships.md +++ b/docs/tutorial/relationship-attributes/read-relationships.md @@ -6,81 +6,7 @@ Now that we know how to connect data using **relationship Attributes**, let's se First, add a function `select_heroes()` where we get a hero to start working with, and add that function to the `main()` function: -//// tab | Python 3.10+ - -```Python hl_lines="3-7 14" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!} - -# Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:108-111]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-7 14" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!} - -# Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:110-113]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-7 14" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-100]!} - -# Previous code here omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:110-113]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,108:111] hl[94:98,111] *} ## Select the Related Team - Old Way @@ -88,69 +14,7 @@ Now that we have a hero, we can get the team this hero belongs to. With what we have learned **up to now**, we could use a `select()` statement, then execute it with `session.exec()`, and then get the `.first()` result, for example: -//// tab | Python 3.10+ - -```Python hl_lines="9-12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-103]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9-12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-105]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-12" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-105]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:103] hl[100:103] *} ## Get Relationship Team - New Way @@ -158,81 +22,7 @@ But now that we have the **relationship attributes**, we can just access them, a So, the highlighted block above, has the same results as the block below: -//// tab | Python 3.10+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!} - - # Code from the previous example omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:105]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!} - - # Code from the previous example omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:107]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-100]!} - - # Code from the previous example omitted 👈 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:107]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,105] hl[105] *} /// tip @@ -246,69 +36,7 @@ For example, here, **inside** a `with` block with a `Session` object. And the same way, when we are working on the **many** side of the **one-to-many** relationship, we can get a list of of the related objects just by accessing the relationship attribute: -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:94-100]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:96-102]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:96-102]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[94:100] hl[100] *} That would print a list with all the heroes in the Preventers team: diff --git a/docs/tutorial/relationship-attributes/remove-relationships.md b/docs/tutorial/relationship-attributes/remove-relationships.md index 56745850..ff408e20 100644 --- a/docs/tutorial/relationship-attributes/remove-relationships.md +++ b/docs/tutorial/relationship-attributes/remove-relationships.md @@ -8,135 +8,11 @@ And then for some reason needs to leave the **Preventers** for some years. 😭 We can remove the relationship by setting it to `None`, the same as with the `team_id`, it also works with the new relationship attribute `.team`: -//// tab | Python 3.10+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:103-114]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:105-116]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:105-116]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[103:114] hl[109] *} And of course, we should remember to add this `update_heroes()` function to `main()` so that it runs when we call this program from the command line: -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:117-121]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:119-123]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[117:121] hl[121] *} ## Recap diff --git a/docs/tutorial/relationship-attributes/type-annotation-strings.md b/docs/tutorial/relationship-attributes/type-annotation-strings.md index a798af9a..026036a2 100644 --- a/docs/tutorial/relationship-attributes/type-annotation-strings.md +++ b/docs/tutorial/relationship-attributes/type-annotation-strings.md @@ -2,63 +2,7 @@ In the first Relationship attribute, we declare it with `List["Hero"]`, putting the `Hero` in quotes instead of just normally there: -//// tab | Python 3.10+ - -```Python hl_lines="9" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="11" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9] *} What's that about? Can't we just write it normally as `List[Hero]`? diff --git a/docs/tutorial/select.md b/docs/tutorial/select.md index be66de35..d130b514 100644 --- a/docs/tutorial/select.md +++ b/docs/tutorial/select.md @@ -23,25 +23,7 @@ Things are getting more exciting! Let's now see how to read data from the databa Let's continue from the last code we used to create some data. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/insert/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/insert/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[0] *} We are creating a **SQLModel** `Hero` class model and creating some records. @@ -178,49 +160,7 @@ The first step is to create a **Session**, the same way we did when creating the We will start with that in a new function `select_heroes()`: -//// tab | Python 3.10+ - -```Python hl_lines="3-4" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-35]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-4" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001.py[ln:36-37]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:35] hl[34:35] *} ## Create a `select` Statement @@ -228,95 +168,11 @@ Next, pretty much the same way we wrote a SQL `SELECT` statement above, now we'l First we have to import `select` from `sqlmodel` at the top of the file: -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:1]!} - -# More code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/select/tutorial001.py[ln:1-3]!} - -# More code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py ln[1] hl[1] *} And then we will use it to create a `SELECT` statement in Python code: -//// tab | Python 3.10+ - -```Python hl_lines="7" -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:1]!} - -# More code here omitted 👈 - -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-36]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9" -{!./docs_src/tutorial/select/tutorial001.py[ln:1-3]!} - -# More code here omitted 👈 - -{!./docs_src/tutorial/select/tutorial001.py[ln:36-38]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py ln[1,34:36] hl[36] *} It's a very simple line of code that conveys a lot of information: @@ -347,49 +203,7 @@ I'll tell you about that in the next chapters. Now that we have the `select` statement, we can execute it with the **session**: -//// tab | Python 3.10+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-37]!} - -# More code here later 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001.py[ln:36-39]!} - -# More code here later 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:37] hl[37] *} This will tell the **session** to go ahead and use the **engine** to execute that `SELECT` statement in the database and bring the results back. @@ -427,49 +241,7 @@ The `results` object is an ` to get the rows where a column is **more than** a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial003_py310.py ln[42:47] hl[44] *} That would output: @@ -795,49 +525,7 @@ Notice that it didn't select `Black Lion`, because the age is not *strictly* gre Let's do that again, but with `>=` to get the rows where a column is **more than or equal** to a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial004_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial004.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial004_py310.py ln[42:47] hl[44] *} Because we are using `>=`, the age `35` will be included in the output: @@ -858,49 +546,7 @@ This time we got `Black Lion` too because although the age is not *strictly* gre Similarly, we can use `<` to get the rows where a column is **less than** a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial005_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial005.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial005_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial005.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial005_py310.py ln[42:47] hl[44] *} And we get the younger one with an age in the database: @@ -918,49 +564,7 @@ We could imagine that **Spider-Boy** is even **younger**. But because we don't k Finally, we can use `<=` to get the rows where a column is **less than or equal** to a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial006_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial006.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial006_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial006.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial006_py310.py ln[42:47] hl[44] *} And we get the younger ones, `35` and below: @@ -985,49 +589,7 @@ We can use the same standard Python comparison operators like `<`, `<=`, `>`, `> Because `.where()` returns the same special select object back, we can add more `.where()` calls to it: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial007_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial007.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial007_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial007.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial007_py310.py ln[42:47] hl[44] *} This will select the rows `WHERE` the `age` is **greater than or equal** to `35`, `AND` also the `age` is **less than** `40`. @@ -1068,49 +630,7 @@ age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' As an alternative to using multiple `.where()` we can also pass several expressions to a single `.where()`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial008_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial008.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial008_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial008.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial008_py310.py ln[42:47] hl[44] *} This is the same as the above, and will result in the same output with the two heroes: @@ -1127,93 +647,13 @@ But we can also combine expressions using `OR`. Which means that **any** (but no To do it, you can import `or_`: -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/where/tutorial009_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/where/tutorial009.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial009_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial009.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial009_py310.py ln[1] hl[1] *} And then pass both expressions to `or_()` and put it inside `.where()`. For example, here we select the heroes that are the youngest OR the oldest: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial009_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial009.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial009_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial009.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial009_py310.py ln[42:47] hl[44] *} When we run it, this generates the output: @@ -1264,91 +704,11 @@ We can tell the editor that this class attribute is actually a special **SQLMode To do that, we can import `col()` (as short for "column"): -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/where/tutorial011_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/where/tutorial011.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial011_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial011.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial011_py310.py ln[1] hl[1] *} And then put the **class attribute** inside `col()` when using it in a `.where()`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial011_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial011.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial011_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial011.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial011_py310.py ln[42:47] hl[44] *} So, now the comparison is not: diff --git a/requirements-docs.txt b/requirements-docs.txt index 4e8c9146..31f1a6d0 100644 --- a/requirements-docs.txt +++ b/requirements-docs.txt @@ -16,4 +16,4 @@ cairosvg==2.7.1 # For griffe, it formats with black typer == 0.12.3 mkdocs-macros-plugin==1.0.5 -markdown-include-variants==0.0.3 +markdown-include-variants==0.0.4