Schemas API#

class fastapi_restly.schemas.BaseSchema#

Bases: BaseModel

Thin Pydantic base for ORM-facing Restly schemas.

Equivalent to:

class BaseSchema(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(from_attributes=True)

from_attributes=True lets Pydantic/FastAPI validate objects by attribute when the schema is used directly. Generated Restly routes still serialize through to_response_schema() so Restly-specific behavior such as WriteOnly filtering and relationship-id normalization is applied.

model_config: ClassVar[ConfigDict] = {'from_attributes': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_restly.schemas.IDRef(value: ~typing.Any = <object object>, *, id: ~typing.Annotated[~typing.Any, fr.ReadOnly])#

Bases: IDSchema, Generic[SQLAlchemyModel]

Reference to a row of T by id.

Wire format is the raw id value (e.g. 5); accepts both scalars and {"id": N} dicts on input. The framework validates the referenced row exists and resolves it to the FK column on the way in.

Use this for typical REST APIs where you want task_id: 5 on the wire. For JSON-API or React-Admin-style nested wire format {"id": N}, use IDSchema[T] instead.

products: list[IDRef[Product]] # serializes as [“uuid1”, “uuid2”]

Resolution is an UNSCOPED existence check: the row is fetched by primary key only, with no view build_query scoping (tenant, soft-delete, row-level visibility), so a reference to a row the caller cannot otherwise see still resolves. If references must respect visibility, gate them in authorize (data.<field>.id is the requested id, before resolution) or before_commit (the resolved row is on the built object). See the IDRef how-to, “Visibility and Multi-Tenancy”.

model_config: ClassVar[ConfigDict] = {'from_attributes': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_restly.schemas.IDSchema(*, id: Annotated[Any, fr.ReadOnly])#

Bases: BaseSchema, Generic[SQLAlchemyModel]

Generic schema useful for serializing only the id of objects. Can be used as IDSchema[MyModel].

get_sql_model_annotation() type[SQLAlchemyModel] | None#

Return the annotation on IDSchema when used as:

foo: IDSchema[Foo]

This property will return “Foo”.

id: ReadOnly, FieldInfo(annotation=NoneType, required=True, json_schema_extra={'readOnly': True})]#
model_config: ClassVar[ConfigDict] = {'from_attributes': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_restly.schemas.TimestampsSchemaMixin(*, created_at: Annotated[datetime, fr.ReadOnly], updated_at: Annotated[datetime, fr.ReadOnly])#

Bases: BaseModel

created_at: ReadOnly, FieldInfo(annotation=NoneType, required=True, json_schema_extra={'readOnly': True})]#
model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

updated_at: ReadOnly, FieldInfo(annotation=NoneType, required=True, json_schema_extra={'readOnly': True})]#
fastapi_restly.schemas.create_schema_from_model(model_cls: type[DeclarativeBase], *, schema_name: str | None = None, include_relationships: bool = True, include_readonly_fields: bool = True) type[BaseSchema]#

Auto-generate a Pydantic schema from a SQLAlchemy model.

Args:

model_cls: The SQLAlchemy model class schema_name: Optional name for the generated schema class include_relationships: Whether to include relationship fields include_readonly_fields: Whether to include read-only fields like id, created_at, etc.

Returns:

A Pydantic schema class

See also

Custom Schemas and Field Types — schema bases, field markers, and aliases in prose; Work with Foreign Keys Using IDRef — the IDRef / IDSchema wire contract.