mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-12-31 06:30:27 -06:00
📝 Update markdown includes format (#1254)
This commit is contained in:
committed by
GitHub
parent
0c65fed61b
commit
5100200bea
@@ -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 <abbr title="Something that can be used in a for loop
|
||||
|
||||
Now we can put it in a `for` loop and print each one of the heroes:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="7-8"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-39]!}
|
||||
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="7-8"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial001.py[ln:36-41]!}
|
||||
|
||||
# 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[34:39] hl[38:39] *}
|
||||
|
||||
This will print the output:
|
||||
|
||||
@@ -483,49 +255,7 @@ id=3 name='Rusty-Man' age=48 secret_name='Tommy Sharp'
|
||||
|
||||
Now include a call to `select_heroes()` in the `main()` function so that it is executed when we run the program from the command line:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="14"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-45]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="14"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial001.py[ln:36-47]!}
|
||||
|
||||
# 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:45] hl[45] *}
|
||||
|
||||
## Review The Code
|
||||
|
||||
@@ -585,49 +315,7 @@ But for different reasons you might want to have the full **list of `Hero`** obj
|
||||
|
||||
The special `results` object also has a method `results.all()` that returns a list with all the objects:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="7"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial003_py310.py[ln:34-39]!}
|
||||
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="7"
|
||||
# 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[38] *}
|
||||
|
||||
With this now we have all the heroes in a list in the `heroes` variable.
|
||||
|
||||
@@ -653,49 +341,7 @@ I have been creating several variables to be able to explain to you what each th
|
||||
|
||||
But knowing what is each object and what it is all doing, we can simplify it a bit and put it in a more compact form:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="5"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial004_py310.py[ln:34-37]!}
|
||||
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="5"
|
||||
# Code above omitted 👆
|
||||
|
||||
{!./docs_src/tutorial/select/tutorial004.py[ln:36-39]!}
|
||||
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/select/tutorial004_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/select/tutorial004.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
{* ./docs_src/tutorial/select/tutorial004_py310.py ln[34:37] hl[36] *}
|
||||
|
||||
Here we are putting it all on a single line, you will probably put the select statements in a single line like this more often.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user