Skip to content

Python Mixins ​

The mixin system is the core of fastapi-viewsets. Each mixin adds one or more HTTP endpoints to a viewset class. You compose viewsets by inheriting from the mixins you need.

How it works ​

Every mixin class owns a private APIRouter and registers its endpoints on it using standard FastAPI decorators. The route_viewset decorator later collects all routes from the class hierarchy, resolves generic type parameters, and registers them on your application router.

You implement the actual business logic by overriding the corresponding perform_* method from ImplMixin. Every perform_* method takes a context: Context as its first parameter after self - see Architecture for the request pipeline that builds it, and Context Processors for how to use it.

Individual operation mixins ​

Create ​

ClassEndpointDescription
CreateMixin[K, T]POST /Create a single record
BulkOnlyCreateMixin[K, T]POST /bulkCreate multiple records
BulkCreateMixin[K, T]both aboveSingle + bulk create
python
from fastapi_viewsets.context import Context
from fastapi_viewsets.mixins import BulkCreateMixin

class MyViewSet(BulkCreateMixin[int, Item]):
    async def perform_create(self, context: Context, data: Item) -> Item:
        db.append(data)
        return data

    async def perform_bulk_create(self, context: Context, data: list[Item]) -> list[Item]:
        db.extend(data)
        return data

With pk_field_name set on route_viewset, the POST / body carries no primary key, and every other field is validated as Item declares it. perform_create receives an Item without the primary key unless Item gives it a default. See pk_field_name and request body.

List & Retrieve ​

ClassEndpointDescription
ListMixin[T, TFilter]GET /Return all records, with optional filter and sort
RetrieveMixin[K, T]GET /{pk}Return a single record by PK

ListMixin is generic over an optional TFilter type (default: None). The endpoint always exposes a sort query parameter (comma-separated column:direction pairs). When a filter model is also provided, its fields appear as individual query parameters, with their types, descriptions, defaults and constraints in the schema; a sequence-typed field (list[int]) is read from a repeated key (?ids=1&ids=2).

Filtering (active when any filter field is set): filter_list(fltr, records) filters in memory. It has no default implementation, so a viewset that declares a filter type implements it — or declares the filter with make_filter_model, which needs no code at all. See Declarative filters.

Sorting (active when the sort query parameter is non-empty, or is absent and the viewset declares default_sort): sort_list(sort, records) sorts in memory. The default is a stable multi-key sort; NULLs sit where the viewset's nulls says, lowest in both directions by default. It compares strings by the viewset's collation and other values as they are; a backend that sorts in the database (DjangoORMViewSet) applies the database's collation instead.

To do either in the backend instead, override apply_filter or apply_sort — see the list pipeline.

python
from fastapi_viewsets.context import Context
from fastapi_viewsets.mixins import ListMixin, RetrieveMixin, make_all_optional, SortState
from fastapi_viewsets.exceptions import NotFoundError

ItemFilter = make_all_optional(Item)

class MyViewSet(ListMixin[Item, ItemFilter], RetrieveMixin[int, Item]):
    async def perform_list(self, context: Context) -> list[Item]:
        return list(db.values())

    async def filter_list(self, fltr: ItemFilter, records: list[Item]) -> list[Item]:
        # filter_list does not receive context - see Context Processors "Known limitations"
        if fltr.name is not None:
            records = [r for r in records if fltr.name.lower() in r.name.lower()]
        return records

    async def perform_retrieve(self, context: Context, pk: int) -> Item:
        if pk not in db:
            raise NotFoundError(pk)
        return db[pk]

Sorting works out of the box without any overrides:

text
GET /items?sort=name:asc,score:desc

default_sort orders a request that sends no sort. It is published as the sort parameter's default in /schema, where the TS client reads it with defaultSort(), e.g. as a grid's initial sortState. An empty sort= leaves the list unordered:

python
class CountryViewSet(CollectionViewSet[int, Country], ListMixin[Country, None]):
    default_sort = "name:asc"

Text order ​

The in-memory sort and the cursor compare strings by the keys of a collation and other values as they are. NULLs never reach the collation. A collation is any object with a sort_key(text) method whose results compare with each other:

python
from fastapi_viewsets.collation import Collation  # Protocol: sort_key(self, text: str) -> Any

The default collation, get_collation(), compares the code points of the str.casefold() form of a string, with digit runs by value. It is case-insensitive, and ß compares as ss. Accented letters are not moved next to their base letters, so Č sorts after z. Equal keys after folding are ordered by the string itself, so only equal strings get equal keys:

text
apple, Banana, Cesta, cesta, eclair, file2, file10, sa, Sz, zebra, zz, Éclair, čas, Čebula, Ša, Žaba

A digit run of up to 640 digits compares by value; a longer run compares as consecutive runs of 640 digits.

get_collation(locale) returns an ICU collation of the locale with numeric collation on. It needs the icu extra (PyICU, which builds against the system ICU; see Installation):

python
from fastapi_viewsets.collation import get_collation

class WordViewSet(CollectionViewSet[int, Word], CursorListMixin[Word, None]):
    collation = get_collation("sl")
text
apple, Banana, cesta, Cesta, čas, Čebula, eclair, Éclair, file2, file10, sa, Sz, Ša, zebra, zz, Žaba

get_collation(locale, on_mismatch="warn"):

  • creates the collation of each (locale, on_mismatch) once and returns the same instance afterwards; creation and its checks run under a lock;
  • without PyICU returns the default collation and issues a CollationWarning;
  • creates the ICU collator of the canonical locale: iw is he, in is id, sh is sr_Latn;
  • raises ValueError for a string ICU does not read as a locale, such as @@@;
  • checks the locale ICU loads collation data for (its valid locale). ICU's root collation for a language that is not root is a mismatch. Any other collation ICU loads is no mismatch, including the one ICU assigns to another language code (yue loads zh_Hant, no loads no or nb). "", root and und request the root collation and are no mismatch. On a mismatch, on_mismatch decides: "use" uses ICU's collation without a warning, "warn" uses it and issues a CollationWarning, "fallback" issues a CollationWarning and returns the default collation.

Each warning is issued once, when the collation is created. An ICU collation keeps one ICU collator per thread. Its keys are (bytes, str): ICU's sort key, then the string for strings ICU ranks equal.

collation_for(context, column_name) returns the collation of a column. The default returns the viewset's collation. Override it for an order per column or per request. It is a coroutine, so it can read context values; context is None when sort_list is called outside get_list:

python
class CodePoint:
    def sort_key(self, text: str) -> str:
        return text

class WordViewSet(CollectionViewSet[int, Word], CursorListMixin[Word, None]):
    async def collation_for(self, context: Context | None, column_name: str) -> Collation:
        if column_name == "code":
            return CodePoint()
        if context is not None and "language" in context:
            return get_collation(await context.language, on_mismatch="fallback")
        return self.collation

The sort and the cursor predicate use the same keys, so cursor pages follow the sort's order. The cursor appends the primary key to every ordering; for a string primary key the raw value follows its collation key, so distinct primary keys never compare equal.

The collation applies only to the in-memory stages. DjangoORMViewSet sorts and pages in SQL with the database's collation. When a filter or a sort column has no SQL translation, its sort and cursor run in memory, and its collation_for returns code point order; collation does not apply.

Sorting in the backend ​

For server-side sorting, override apply_sort, order the records in the backend and mark the stage applied so the in-memory sort leaves them alone:

python
async def apply_sort(self, context, query, records):
    if query.has_sort:
        records = records.order_by(*(column.column_name for column in query.sort))
        query.mark_applied("sort")
    return await super().apply_sort(context, query, records)

Update ​

ClassEndpointDescription
UpdateMixin[K, T]PUT /{pk}, PATCH /{pk}Full and partial update
BulkOnlyUpdateMixin[K, T]PUT /bulk, PATCH /bulkBulk full and partial update
BulkUpdateMixin[K, T]all four aboveSingle + bulk update

The partial parameter is False for PUT and True for PATCH.

A PUT body is the whole model. A PATCH body states the fields it changes: every field is optional, the primary key is not part of it, and a field it states is validated by its annotation, its constraints and the model's @field_validators. The model's @model_validators are not applied: they validate a whole model, and a partial body is not one. DjangoORMViewSet applies them to the stored row with the stated fields set on it (Backends). perform_update receives it as the model holding the stated fields only: data.model_fields_set names them, and data.model_dump(exclude_unset=True) is what the request stated; PATCH /bulk does the same for each record. A field read with getattr keeps a nested model as the model; model_dump turns it into a dict.

python
async def perform_update(self, context: Context, pk: int, data: Item, partial: bool = True) -> Item:
    if partial:
        db[pk] = db[pk].model_copy(update={name: getattr(data, name) for name in data.model_fields_set})
    else:
        db[pk] = data
    return db[pk]

Destroy ​

ClassEndpointDescription
DestroyMixin[K, T]DELETE /{pk}Delete a single record
BulkOnlyDestroyMixin[K, T]DELETE /bulkDelete multiple records
BulkDestroyMixin[K, T]both aboveSingle + bulk delete

perform_destroy deletes one record and returns {pk: None}; a missing record raises NotFoundError (404).

bulk_destroy deletes each key independently of the others and answers 200 with an object keyed by pk: null for a deleted record, the ApiErrorBody of the failure otherwise. A failed key does not stop the rest.

json
{"1": null, "9": {"detail": "Item with pk 9 not found", "detail_code": "fastapi-viewsets.not_found",
       "detail_params": {"pk": 9}, "field_errors": {}}}

ImplMixin.perform_bulk_destroy implements this over perform_destroy: an HTTPException, a RequestValidationError or Django's ValidationError becomes that key's ApiErrorBody, any other exception propagates (500), and a repeated key is deleted once. Override it to delete in one statement or one transaction.

Lookup ​

LookupMixin[TLookupFilter, TLookupItem] adds a GET /lookup endpoint that returns a list of TLookupItem objects — useful for populating select/autocomplete widgets. TLookupFilter defaults to LookupFilter and TLookupItem to LookupItem (group, pk, title, icon). The fields of TLookupFilter are the endpoint's query parameters. LookupFilter has two: q, a search in the title, and ids, primary keys read from a repeated key (?ids=1&ids=5). The TS lookupChoices sends q for a select's search and ids for the labels of its value.

A lookup request runs through the same stages as a list:

  1. perform_lookup(context) returns the source: a list, a generator or an unevaluated QuerySet.
  2. apply_lookup_filter(context, query, records) narrows it when any filter field is set. The filter is query.fltr. Override the stage to filter in the backend and call query.mark_applied("filter"); the default then leaves the records as they are. Without an override, the default filters the converted items in memory: by the declaration of a filter model built with make_filter_model, otherwise through filter_lookup(fltr, items), whose default keeps the items whose title contains fltr.q, ignoring case, and whose pk is in fltr.ids, compared as strings (str(item.pk)); a field that is not set does not filter.
  3. to_lookup_item(raw) converts each row to TLookupItem. The default is identity.

The filter is passed through query, so concurrent requests on one viewset instance (the default singleton lifecycle) do not share it.

The endpoint answers in the order of the source; filtering and conversion keep it. perform_lookup returns the items ordered by group, then by their text, so that a select shows each group's items together. Ordering in the source applies its collation: Tag.objects.order_by("name") orders by the database's, so Č follows C under a Slovenian collation. The example below orders in memory with a collation.

python
from fastapi_viewsets.collation import get_collation
from fastapi_viewsets.mixins import LookupMixin, LookupItem, LookupFilter

_slovenian = get_collation("sl")

# Default filter: ?q= searches the title, ?ids=1&ids=5 selects by pk
class MyViewSet(..., LookupMixin):
    async def perform_lookup(self, context: Context) -> list[LookupItem]:
        items = sorted(db.values(), key=lambda i: _slovenian.sort_key(i.name))
        return [LookupItem(group=None, pk=i.id, title=i.name, icon=None) for i in items]

A lookup with its own item type, q and ids parameters, filtered in the database:

python
class TagLookup(BaseModel):
    id: int
    name: str
    color: str

class TagLookupFilter(BaseModel):
    q: str | None = None
    ids: list[int] | None = None  # ?ids=1&ids=2

class TagViewSet(..., LookupMixin[TagLookupFilter, TagLookup]):
    async def perform_lookup(self, context: Context):
        return Tag.objects.order_by("name")

    async def apply_lookup_filter(self, context, query, records):
        if query.has_filter:
            if query.fltr.q:
                records = records.filter(name__icontains=query.fltr.q)
            if query.fltr.ids:
                records = records.filter(id__in=query.fltr.ids)
            query.mark_applied("filter")
        return await super().apply_lookup_filter(context, query, records)

    def to_lookup_item(self, raw: Tag) -> TagLookup:
        return TagLookup(id=raw.id, name=raw.name, color=raw.color)

A filter model built with make_filter_model(TLookupItem, {...}) is applied without code, in memory. Its operators compare values of the field's declared type, so LookupItem.pk, declared as object, takes no typed comparison: ?pk__in=1&pk__in=2 compares strings.

Combined viewset mixins ​

For convenience, three pre-composed classes are provided:

ClassIncluded actions
ReadOnlyViewSetMixin[K, T]list, retrieve
ViewSetMixin[K, T, TFilter=None]list, retrieve, create, update, partial_update, destroy
BulkViewSetMixin[K, T, TFilter=None]all of the above + bulk_create, bulk_update, bulk_partial_update, bulk_destroy
python
from fastapi_viewsets.mixins import BulkViewSetMixin, make_all_optional

ItemFilter = make_all_optional(Item)

class ItemViewSet(BulkViewSetMixin[int, Item, ItemFilter]):
    async def perform_list(self, context): ...
    async def perform_retrieve(self, context, pk): ...
    async def perform_create(self, context, data): ...
    async def perform_bulk_create(self, context, data): ...
    async def perform_update(self, context, pk, data, partial): ...
    async def perform_bulk_update(self, context, records, partial): ...
    async def perform_destroy(self, context, pk): ...
    async def filter_list(self, fltr, records): ...  # not context-threaded, see Context Processors

Error handling ​

Raise NotFoundError when a record does not exist. It produces a proper 404 response with a JSON body.

python
from fastapi_viewsets.exceptions import NotFoundError

raise NotFoundError(pk)
# → HTTP 404: {"detail": "Item with pk 42 not found", "detail_code": "fastapi-viewsets.not_found", "detail_params": {"pk": 42}, "field_errors": {}}

Released under the MIT License.