Skip to content

Models

Shared Pydantic model base classes and the ErrorDetails / ServerSideEvent types. Import from fastapi_views.models.

Base schemas

Class Description
BaseSchema Pydantic BaseModel with use_enum_values, populate_by_name, and from_attributes enabled
CamelCaseSchema BaseSchema with alias_generator = to_camel for camelCase JSON keys
IdSchema BaseSchema with a UUID id field
CreatedUpdatedSchema BaseSchema with created_at and updated_at datetime fields
IdCreatedUpdatedSchema Combines IdSchema and CreatedUpdatedSchema

Error model

ErrorDetails is the base Pydantic model for all RFC 9457 error responses. When OpenTelemetry is installed, it conditionally gains a correlation_id field populated from the active trace context.

Server-Sent Events

ServerSideEvent[D] is the generic model that wraps each SSE payload. Its get_openapi_schema() class method returns an OpenAPI-compatible schema dict used by ServerSideEventsAPIView when registering routes.


ErrorDetails

Bases: BaseSchema

Base Model for https://www.rfc-editor.org/rfc/rfc9457.html

Source code in fastapi_views/models.py
class ErrorDetails(BaseSchema):
    """Base Model for https://www.rfc-editor.org/rfc/rfc9457.html"""

    @classmethod
    def new(cls: type[Self], detail: str, **kwargs: Any) -> Self:
        return cls(detail=detail, **kwargs)

    type: Url | Literal["about:blank"] = Field(
        "about:blank",
        description="Error type",
    )
    title: str = Field(description="Error title")
    status: int = Field(description="Error status")
    detail: str = Field(description="Error detail")
    instance: str | None = Field(None, description="Requested instance")

    if has_opentelemetry():
        correlation_id: str | None = Field(
            description="Optional correlation id",
            default_factory=get_correlation_id,
        )

    errors: list[Any] = Field([], description="List of any additional errors")