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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions docs/database/casts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords: casts, serialization, model attributes, pydantic, json, orm

# Casts

Casts automatically transform model attribute values when reading from or writing to the database. Annotate a field with the desired type and the ORM handles the conversion transparently.
Casts automatically transform model attribute values when reading from or writing to the database. Annotate a field with the desired type and the ORM handles the conversion transparently. The optional `Field[T]()` syntax also supports explicit field types.

## Built-in Casts

Expand Down Expand Up @@ -152,17 +152,17 @@ class User(Model):

A `NULL` database value will be returned as `time(9, 0, 0)` instead of `None`.

## Custom Casts with `Attribute`
## Nested Pydantic Models

For complex types — such as embedded value objectsextend `Attribute`. It is a Pydantic `BaseModel` subclass, so fields are validated and the instance is serialized to JSON automatically.
For embedded value objects, extend Pydantic's `BaseModel` and annotate the ORM field as `address: Address`. Values are reconstructed as Pydantic models when read and serialized to JSON when written.

### Defining a custom cast

```python
from typing import Optional
from fastapi_startkit.masoniteorm import Attribute
from pydantic import BaseModel

class Address(Attribute):
class Address(BaseModel):
street: Optional[str] = None
city: Optional[str] = None
state: Optional[str] = None
Expand All @@ -171,7 +171,7 @@ class Address(Attribute):

### Wiring it to a model

Annotate the field with your `Attribute` subclass:
Use your Pydantic model as the field type:

```python
from fastapi_startkit.masoniteorm import Model
Expand All @@ -183,6 +183,12 @@ class User(Model):
address: Address
```

The legacy declaration `address: Address = ModelField()` remains supported.
`ModelField` is publicly importable from `fastapi_startkit.masoniteorm`, but
emits a `DeprecationWarning` and is scheduled for removal in **2.x**. Migrate to
`address: Address`. If you prefer explicit descriptors, import `Field` and use
`address = Field[Address]()` instead.

The column should be a `text` or `json` column in your migration:

```python
Expand Down
4 changes: 2 additions & 2 deletions docs/database/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Models represent database tables and are the primary interface for reading and w

## Defining a Model

Extend `Model` from `fastapi_startkit.masoniteorm` and annotate your columns as class-level type hints:
Extend `Model` from `fastapi_startkit.masoniteorm` and annotate your columns with Python types:

```python
from fastapi_startkit.masoniteorm import Model
Expand Down Expand Up @@ -234,4 +234,4 @@ class Post(Model):

Relationship declarations are covered in the [Relationships](./relationships) section.

Automatic type coercion and custom value objects are covered in the [Casts](./casts) section.
Automatic type coercion and custom value objects are covered in the [Casts](./casts) section.