@dynamicforms/fastapi-viewsets

中文文档
0.15.4发布于 2026/11/13MITTypeScriptES Module零依赖近 30 天下载 2833

RESTful viewsets for Vue

原始使用文档
文档目录4 节
  1. Installation
  2. Quick example
  3. Demo
  4. License

DynamicForms FastAPI Viewsets

Django REST Framework-style viewsets for FastAPI, with optional Celery-backed async execution and a matching Vue/TypeScript client counterpart.

  • Python mixins for FastAPI — compose CRUD and bulk endpoints from small, focused mixin classes.
  • route_viewset decorator — register a viewset on a FastAPI router with a single decorator call. Handles type resolution, lifecycle management and OpenAPI schema automatically.
  • Nested viewsets — mount a viewset under another one's path (/accounts/members) with ViewSetRouter.sub_viewset; a request to it passes the parent's checks and its own.
  • CollectionViewSet — zero-boilerplate in-memory viewset backed by any Python list, set or dict. Great for prototyping and testing.
  • DjangoORMViewSet — back a viewset with a Django QuerySet. The filters it can compile, the sort order with its NULL placement, the page as LIMIT/OFFSET and the total as COUNT(*) all become SQL; a stage it declines falls back to the in-memory pass instead of failing, and pk_field_name comes from the model's primary key (requires the django extra).
  • celery_viewset decorator — move a viewset's execution to a Celery worker with no code changes to the viewset itself, for long-running or background processing scenarios (requires the celery extra).
  • Bulk operations — first-class support for bulk create, update, partial update and destroy.
  • muxws transport — reach the same viewsets over a single WebSocket instead of one HTTP request per call. A command goes through your application as an HTTP request does - its middleware, exception handlers, router dependencies and state - so both transports behave identically.
  • Events — publish an event to a topic and every frontend following it learns of it, as a muxws push or on its next poll. ChangeEvents publishes every create, update and destroy of a viewset to its routed base path; custom events are declared with source(). Every event is recorded before it is pushed, so a dropped push costs latency, not an event. On the frontend, watch() on a ViewSet follows its topic and applyChanges() applies each change to a list of rows.
  • Three list shapes — a bare array, offset paging, or cursor paging. A viewset declares which, and may let a client pick per request with an X-List-Shape header.
  • Declarative filters — declare which fields accept which operators and get query parameters, an OpenAPI schema and filtering for free; backends translate what they can into their own query.
  • Vue / TypeScript counterpart — mirror mixin classes and the restViewSet / muxwsViewSet class factory give you a fully typed client that matches your backend viewset exactly: the mixins a ViewSet declares are its public surface, so calling an action it did not declare is a compile error rather than a runtime 404 (published separately as @dynamicforms/fastapi-viewsets on npm).

Installation

pip install dynamicforms-fastapi-viewsets

# with Celery-backed viewset support
pip install "dynamicforms-fastapi-viewsets[celery]"

# with the muxws WebSocket transport
pip install "dynamicforms-fastapi-viewsets[muxws]"

# with the Django ORM backend (Django 4.2+ and asgiref)
pip install "dynamicforms-fastapi-viewsets[django]"

Requires Python 3.10+, FastAPI and Pydantic 2.11+.

Quick example

from fastapi import APIRouter, FastAPI
from pydantic import BaseModel

from fastapi_viewsets.collection_viewset import CollectionViewSet
from fastapi_viewsets.decorators.route_viewset import route_viewset
from fastapi_viewsets.mixins import BulkViewSetMixin


class Item(BaseModel):
    id: int
    name: str


database: dict[int, Item] = {1: Item(id=1, name="First element")}

app = FastAPI()
router = APIRouter()


@route_viewset(router, base_path="/items", pk_field_name="id")
class ItemViewSet(CollectionViewSet[int, Item], BulkViewSetMixin[int, Item]):
    def __init__(self):
        super().__init__(container=database, pk_field="id")


app.include_router(router)

See the full documentation for guides on the mixin system, route_viewset, CollectionViewSet, DjangoORMViewSet, celery_viewset, the list pipeline and pagination, the muxws transport, events, and the Vue client.

Demo

uv sync && npm ci
uv run python demo.py                # backend on :8000, frontend on :5173
uv run python demo.py --celery       # ... with every viewset call routed through a Celery worker
npm run test:e2e                     # drives the demo in a browser

An infinite-scrolling grid over a 5000-track library, cursor-paged. Switch the whole grid between the REST and muxws transports and between the in-memory and SQLite backends, and compare their latency side by side. Sorting and filtering are server-side.

The grid follows its viewset's change events (ChangeEvents on the backend, watch() in the grid). Click a Favorite cell to toggle it: every tab showing the library applies the change, over muxws as a push, over REST on its next poll. A sorted or filtered grid reloads its list on the change: the row may have moved or left it.

--celery needs Redis on localhost:6379. The worker runs the viewset calls of both backends and publishes their events through Redis. It runs as one process, because the in-memory library is a dict held by each process; the SQLite library is one file shared by every process. The end-to-end suite starts its own backend and dev server on their own ports.

License

MIT — see LICENSE.

版本与发布记录

@dynamicforms/fastapi-viewsets 共发布 42 个版本,首次发布于 2026/07/10;过去 12 个月发布了 42 个版本。

版本发布日期距今
0.15.4latest2026/11/13今天
0.15.32026/11/13今天
0.15.22026/11/13今天
0.15.12026/11/13今天
0.15.02026/11/121 天前
0.14.22026/10/2816 天前

关于 @dynamicforms/fastapi-viewsets 的常见问题

@dynamicforms/fastapi-viewsets 是什么?

RESTful viewsets for Vue

如何安装 @dynamicforms/fastapi-viewsets?

在项目目录运行 npm install @dynamicforms/fastapi-viewsets,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:axios(^1.13.6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dynamicforms/fastapi-viewsets 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dynamicforms/fastapi-viewsets 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dynamicforms/fastapi-viewsets 可以商用吗?

@dynamicforms/fastapi-viewsets 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dynamicforms/fastapi-viewsets 的下载量有多少?

根据 npm 官方下载统计,@dynamicforms/fastapi-viewsets 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,833 次,日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dynamicforms/fastapi-viewsets 还在维护吗?

最新版本 0.15.4 发布于 2026/11/13(今天);至今共发布 42 个版本,其中过去 12 个月发布了 42 个;首次发布于 2026/07/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。