Skip to content

Commit

Permalink
📝 Update docs for Decimal, use proper types (fastapi#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Dec 4, 2023
1 parent 50b0198 commit 41495e3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
14 changes: 3 additions & 11 deletions docs/advanced/decimal.md
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
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
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

0 comments on commit 41495e3

Please sign in to comment.