API Reference
Pydantic
pydantic
Pydantic-backed JSON field for Tortoise ORM.
PydanticJSONField
Bases: JSONField[dict[str, object]]
JSONField that round-trips values through a Pydantic model.
On save, serializes BaseModel instances with model_dump(mode="json")
before JSON encoding. On load, validates decoded data with the configured
Pydantic model, including:
- JSON strings or bytes (typical for SQLite).
- Plain dicts returned by PostgreSQL JSONB drivers such as asyncpg.
Examples:
>>> class MyModel(Model):
... data: PydanticJSONField[MyPydanticModel] = PydanticJSONField(
... MyPydanticModel, default=MyPydanticModel
... )
Source code in src/tortoise_extensions/pydantic.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
__init__(model_class: type[T], **kwargs: Any) -> None
Initialize the field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
type[T]
|
Pydantic model class used for validation. |
required |
**kwargs
|
Any
|
Additional arguments forwarded to |
{}
|
Source code in src/tortoise_extensions/pydantic.py
29 30 31 32 33 34 35 36 37 | |
to_python_value(value: Any) -> T | None
Convert a database value to a Pydantic model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Raw value from the database driver. |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
A validated Pydantic model, or |
Source code in src/tortoise_extensions/pydantic.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
to_db_value(value: Any, instance: type[Model] | Model) -> str | None
Convert a Python value to a JSON string for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Model instance or JSON-serializable data. |
required |
instance
|
type[Model] | Model
|
Tortoise model class or instance (unused). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Encoded JSON string, or |
Source code in src/tortoise_extensions/pydantic.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
UUID6
uuid6
UUID v6+ field for Tortoise ORM (via the uuid6 package).
FutureUUIDField
Bases: Field[UUID], UUID
UUID field compatible with UUID v6+ and the uuid6 package.
Stores UUIDs as CHAR(36) on most backends and as native UUID on
PostgreSQL. When used as a primary key without an explicit default,
assigns UUID7 via uuid6.uuid7.
Examples:
>>> class Item(Model):
... id: FutureUUIDField = FutureUUIDField(primary_key=True)
... class Meta:
... table = "items"
Source code in src/tortoise_extensions/uuid6.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
__init__(**kwargs: Any) -> None
Initialize the field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Tortoise field options. Primary keys without |
{}
|
Source code in src/tortoise_extensions/uuid6.py
31 32 33 34 35 36 37 38 39 40 41 42 | |
to_db_value(value: Any, instance: type[Model] | Model) -> str | None
Convert a UUID to its string database representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
UUID value or other supported input. |
required |
instance
|
type[Model] | Model
|
Tortoise model class or instance (unused). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
String UUID, or |
Source code in src/tortoise_extensions/uuid6.py
44 45 46 47 48 49 50 51 52 53 54 55 | |
to_python_value(value: Any) -> UUID | None
Convert a database value to a uuid6.UUID instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Raw value from the database driver. |
required |
Returns:
| Type | Description |
|---|---|
UUID | None
|
Parsed UUID, or |
Source code in src/tortoise_extensions/uuid6.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |