]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
📝 Update docs for Decimal, use proper types (#719)
authorSebastián Ramírez <tiangolo@gmail.com>
Mon, 4 Dec 2023 09:49:23 +0000 (10:49 +0100)
committerGitHub <noreply@github.com>
Mon, 4 Dec 2023 09:49:23 +0000 (09:49 +0000)
docs/advanced/decimal.md
docs_src/advanced/decimal/tutorial001.py
docs_src/advanced/decimal/tutorial001_py310.py

index 036aae0003c6125436768e58ede676faaf1089e0..2b58550d7f2cf4b4e4b90347ecb8120cb44052ce 100644 (file)
@@ -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>.
 
 ///
 
index b803119d9e1f8a803bdb3280c6f9b278f31d11f9..a0a9804ade2ca1bc0f7613e29164a08c147ab08a 100644 (file)
@@ -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
 
 
@@ -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"
index 92afc09012e4b59bfce4cbc3f9ae8880ae83cf94..267338912e073179d88787a831d681b022fb461a 100644 (file)
@@ -1,4 +1,5 @@
-from pydantic import condecimal
+from decimal import Decimal
+
 from sqlmodel import Field, Session, SQLModel, create_engine, select
 
 
@@ -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"