Schemas API#

class fastapi_restly.schemas.BaseSchema#

Bases: BaseModel

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() 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.IDStampsSchema(*, id: Annotated[Any, fr.ReadOnly], created_at: Annotated[datetime, fr.ReadOnly], updated_at: Annotated[datetime, fr.ReadOnly])#

Bases: TimestampsSchemaMixin, IDSchema

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.OmitReadOnlyMixin#

Bases: BaseModel

Mixin for pydantic models that removes all fields marked as ReadOnly.

model_config: ClassVar[ConfigDict] = {}#

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

class fastapi_restly.schemas.PatchMixin#

Bases: BaseModel

A mixin for pydantic classes that makes all fields optional and replaces defaults with None.

model_config: ClassVar[ConfigDict] = {}#

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

fastapi_restly.schemas.get_writable_inputs(schema_obj: BaseSchema, schema_cls: type[BaseModel] | None = None) dict[str, Any]#

Return a dictionary of field_name: value pairs for writable input fields.

Filters out: - ReadOnly fields - fields not provided with input (using Pydantic model_fields_set)

Args:

schema_obj: The schema object to extract writable fields from schema_cls: The schema class to check for readonly fields. If None, uses schema_obj.__class__

Returns:

Dictionary mapping field names to their values for writable input fields only

fastapi_restly.schemas.is_readonly_field(model: BaseModel | type[BaseModel], field_name: str) bool#

Check if a specific field is marked as readonly.

fastapi_restly.schemas.resolve_ids_to_sqlalchemy_objects(session: Session, schema_obj: BaseSchema) None#

Go over the Pydantic fields and turn any IDSchema objects into SQLAlchemy instances. A database request is made for each IDSchema to look up the related row in the database. If an id is not found in the database sqlalchemy.orm.exc.NoResultFound is raised.