FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

馃摑 Update docs for Decimal, use proper types by tiangolo 路 Pull Request #719 路 fastapi/sqlmodel 路 GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 3 additions & 11 deletions docs/advanced/decimal.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ In most cases this would probably not be a problem, for example measuring views

## Decimal Types

Pydantic has special support for `Decimal` types using the <a href="https://pydantic-docs.helpmanual.io/usage/types/#arguments-to-condecimal" class="external-link" target="_blank">`condecimal()` special function</a>.
Pydantic has special support for <a href="https://docs.pydantic.dev/latest/api/standard_library_types/#decimaldecimal" class="external-link" target="_blank">`Decimal` types</a>.

/// tip

Pydantic 1.9, that will be released soon, has improved support for `Decimal` types, without needing to use the `condecimal()` function.

But meanwhile, you can already use this feature with `condecimal()` in **SQLModel** it as it's explained here.

///

When you use `condecimal()` you can specify the number of digits and decimal places to support. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
When you use `Decimal` you can specify the number of digits and decimal places to support in the `Field()` function. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.

/// info

For the database, **SQLModel** will use <a href="https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.DECIMAL" class="external-link" target="_blank">SQLAlchemy's `DECIMAL` type</a>.
For the database, **SQLModel** will use <a href="https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.DECIMAL" class="external-link" target="_blank">SQLAlchemy's `DECIMAL` type</a>.

///

Expand Down
4 changes: 2 additions & 2 deletions docs_src/advanced/decimal/tutorial001.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from decimal import Decimal
from typing import Optional

from pydantic import condecimal
from sqlmodel import Field, Session, SQLModel, create_engine, select


Expand All @@ -9,7 +9,7 @@ class Hero(SQLModel, table=True):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
money: condecimal(max_digits=5, decimal_places=3) = Field(default=0)
money: Decimal = Field(default=0, max_digits=5, decimal_places=3)


sqlite_file_name = "database.db"
Expand Down
5 changes: 3 additions & 2 deletions docs_src/advanced/decimal/tutorial001_py310.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import condecimal
from decimal import Decimal

from sqlmodel import Field, Session, SQLModel, create_engine, select


Expand All @@ -7,7 +8,7 @@ class Hero(SQLModel, table=True):
name: str = Field(index=True)
secret_name: str
age: int | None = Field(default=None, index=True)
money: condecimal(max_digits=5, decimal_places=3) = Field(default=0)
money: Decimal = Field(default=0, max_digits=5, decimal_places=3)


sqlite_file_name = "database.db"
Expand Down

Back | FazBrowse Home | New Git URL