Skip to content

Commit

Permalink
πŸ“ Update include markdown docs with new source files for Python 3.9 a…
Browse files Browse the repository at this point in the history
…nd 3.10
  • Loading branch information
tiangolo committed Mar 14, 2024
1 parent e7d2e98 commit 01eac81
Show file tree
Hide file tree
Showing 37 changed files with 7,436 additions and 0 deletions.
82 changes: 82 additions & 0 deletions docs/advanced/decimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,44 @@ For the database, **SQLModel** will use <a href="https://docs.sqlalchemy.org/en/

Let's say that each hero in the database will have an amount of money. We could make that field a `Decimal` type using the `condecimal()` function:

//// tab | Python 3.10+

```{.python .annotate hl_lines="12" }
{!./docs_src/advanced/decimal/tutorial001_py310.py[ln:1-12]!}
# More code here later πŸ‘‡
```

////

//// tab | Python 3.7+

```{.python .annotate hl_lines="12" }
{!./docs_src/advanced/decimal/tutorial001.py[ln:1-12]!}
# More code here later πŸ‘‡
```

////

/// details | πŸ‘€ Full file preview

//// tab | Python 3.10+

```Python
{!./docs_src/advanced/decimal/tutorial001_py310.py!}
```

////

//// tab | Python 3.7+

```Python
{!./docs_src/advanced/decimal/tutorial001.py!}
```

////

///

Here we are saying that `money` can have at most `5` digits with `max_digits`, **this includes the integers** (to the left of the decimal dot) **and the decimals** (to the right of the decimal dot).
Expand Down Expand Up @@ -79,6 +105,20 @@ Make sure you adjust the number of digits and decimal places for your own needs,

When creating new models you can actually pass normal (`float`) numbers, Pydantic will automatically convert them to `Decimal` types, and **SQLModel** will store them as `Decimal` types in the database (using SQLAlchemy).

//// tab | Python 3.10+

```Python hl_lines="4-6"
# Code above omitted πŸ‘†

{!./docs_src/advanced/decimal/tutorial001_py310.py[ln:25-35]!}

# Code below omitted πŸ‘‡
```

////

//// tab | Python 3.7+

```Python hl_lines="4-6"
# Code above omitted πŸ‘†

Expand All @@ -87,18 +127,46 @@ When creating new models you can actually pass normal (`float`) numbers, Pydanti
# Code below omitted πŸ‘‡
```

////

/// details | πŸ‘€ Full file preview

//// tab | Python 3.10+

```Python
{!./docs_src/advanced/decimal/tutorial001_py310.py!}
```

////

//// tab | Python 3.7+

```Python
{!./docs_src/advanced/decimal/tutorial001.py!}
```

////

///

## Select Decimal data

Then, when working with Decimal types, you can confirm that they indeed avoid those rounding errors from floats:

//// tab | Python 3.10+

```Python hl_lines="15-16"
# Code above omitted πŸ‘†

{!./docs_src/advanced/decimal/tutorial001_py310.py[ln:38-51]!}

# Code below omitted πŸ‘‡
```

////

//// tab | Python 3.7+

```Python hl_lines="15-16"
# Code above omitted πŸ‘†

Expand All @@ -107,12 +175,26 @@ Then, when working with Decimal types, you can confirm that they indeed avoid th
# Code below omitted πŸ‘‡
```

////

/// details | πŸ‘€ Full file preview

//// tab | Python 3.10+

```Python
{!./docs_src/advanced/decimal/tutorial001_py310.py!}
```

////

//// tab | Python 3.7+

```Python
{!./docs_src/advanced/decimal/tutorial001.py!}
```

////

///

## Review the results
Expand Down
Loading

0 comments on commit 01eac81

Please sign in to comment.