📝 Update docs for Decimal, use proper types (#719)
This commit is contained in:
parent
50b0198423
commit
41495e30c7
@ -19,21 +19,13 @@ In most cases this would probably not be a problem, for example measuring views
|
|||||||
|
|
||||||
## Decimal Types
|
## 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
|
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.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
/// info
|
/// 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>.
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
from decimal import Decimal
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import condecimal
|
|
||||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||||
|
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ class Hero(SQLModel, table=True):
|
|||||||
name: str = Field(index=True)
|
name: str = Field(index=True)
|
||||||
secret_name: str
|
secret_name: str
|
||||||
age: Optional[int] = Field(default=None, index=True)
|
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"
|
sqlite_file_name = "database.db"
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from pydantic import condecimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||||
|
|
||||||
|
|
||||||
@ -7,7 +8,7 @@ class Hero(SQLModel, table=True):
|
|||||||
name: str = Field(index=True)
|
name: str = Field(index=True)
|
||||||
secret_name: str
|
secret_name: str
|
||||||
age: int | None = Field(default=None, index=True)
|
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"
|
sqlite_file_name = "database.db"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user