Skip to content

CollectionViewSet — API Reference ​

python
from fastapi_viewsets.collection_viewset import CollectionViewSet

Class hierarchy ​

text
AsyncCollectionViewSet[K, T]  (ImplMixin[K, T], ABC)
└── CollectionViewSet[K, T]

AsyncCollectionViewSet ​

Abstract base. Subclasses must implement the five container_* methods.

python
class AsyncCollectionViewSet(Generic[K, T], ImplMixin[K, T], ABC):
    async def container_list(self) -> list[T]: ...
    async def container_retrieve(self, pk: K) -> T: ...
    async def container_add(self, data: T) -> T: ...
    async def container_update(self, pk: K, data: T, partial: bool) -> T: ...
    async def container_delete(self, pk: K) -> None: ...

All perform_* methods from ImplMixin are implemented here by delegating to the container_* methods.

CollectionViewSet ​

Concrete implementation backed by a Python collection.

Constructor ​

python
CollectionViewSet(container, pk_field="id")
ParameterTypeDefaultDescription
containerIterable[T] | MutableSequence[T] | MutableSet[T] | MutableMapping[K, T]—Backing collection
pk_fieldstr"id"Attribute/key name used as primary key

Supported container types ​

TypeReadWrite
Iterable (non-mutable)✅❌
MutableSequence (e.g. list)✅✅
MutableSet (e.g. set)✅✅
MutableMapping (e.g. dict)✅✅

A write to a non-mutable container raises ReadOnlyCollectionError (405, read_only). A partial update replaces a set's item with a copy (model_copy, _replace or dataclasses.replace) and raises TypeError for an item that is none of a pydantic model, a named tuple or a dataclass.

Auto-increment PK ​

Fields with json_schema_extra={"autoinc_int": True} are automatically assigned the next integer value on create when the field is None or 0.

python
class Item(BaseModel):
    id: int | None = Field(default=None, json_schema_extra={"autoinc_int": True})
    name: str

Errors ​

Raises NotFoundError(pk) (HTTP 404) when a record is not found during retrieve, update, or delete.

Released under the MIT License.