Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Update includes for docs/tutorial/fastapi/update.md #1181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
324 changes: 5 additions & 319 deletions docs/tutorial/fastapi/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,7 @@ Here is one of those cases where it probably makes sense to use an **independent

Because each field is **actually different** (we just change it to `Optional`, but that's already making it different), it makes sense to have them in their own model.

///

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[21:24] *}

This is almost the same as `HeroBase`, but all the fields are optional, so we can't simply inherit from `HeroBase`.

Expand All @@ -94,69 +28,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[3:4] *}

We also read the `hero_id` from the *path parameter* and the request body, a `HeroUpdate`.

Expand All @@ -166,69 +38,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[6:8] *}

### Get the New Data

Expand Down Expand Up @@ -280,69 +90,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[9] *}

/// 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.
Expand All @@ -352,69 +100,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[10] *}

/// tip

Expand Down