Skip to main content

Generate full CRUD REST endpoints for FastAPI straight from your ORM models — filtering, sorting, pagination and nested resources, without hand-writing a single schema. Works with SQLModel, SQLAlchemy and Tortoise ORM.

Project description

fastapi-crud-generator

Tests PyPI Python License: MIT

CRUD route generator for FastAPI. Point it at an ORM model and get a full set of endpoints — with filtering, sorting, pagination, and nested resources — without writing a single Pydantic schema by hand.

Works with SQLModel, SQLAlchemy, and Tortoise ORM.

What it does

  • Auto schemas — public, create, and update schemas are derived from the ORM model automatically
  • Filtering — every scalar field becomes a query parameter: ?status=published&author_id=5
  • Sorting?sort=created_at:desc or multiple: ?sort=published:desc&sort=title:asc
  • Pagination?page=2&per_page=20, response always includes total count
  • Nested resources/threads/{thread_id}/posts/{post_id} in three lines
  • Related data — fetch relations in one request via ?include=author&include=tags
  • Customizable — disable any endpoint, override any handler, replace any schema

Quick start

from fastapi import FastAPI
from fastapi_crud_generator import CRUDCollection
from fastapi_crud_generator.orm.sqlmodel import SQLModelAdapter

app = FastAPI()

crud = CRUDCollection(orm_adapter=SQLModelAdapter(model=Article, get_session=get_session))
app.include_router(crud.get_router(prefix="/articles", tags=["articles"]))

This registers five endpoints:

GET    /articles               # list: filtering, sorting, pagination
GET    /articles/{article_id}  # get one
POST   /articles               # create
PATCH  /articles/{article_id}  # partial update
DELETE /articles/{article_id}  # delete

Request and response schemas are generated from the Article model automatically — no ArticleCreate, ArticleUpdate, or ArticlePublic classes needed.

Nested resources

thread_crud = CRUDCollection(orm_adapter=SQLModelAdapter(model=Thread, get_session=get_session))
post_crud   = CRUDCollection(orm_adapter=SQLModelAdapter(model=Post,   get_session=get_session))

thread_crud.add_nested_collection("/posts", post_crud)
app.include_router(thread_crud.get_router(prefix="/threads"))

This gives you the full hierarchy:

GET    /threads
GET    /threads/{thread_id}
POST   /threads
PATCH  /threads/{thread_id}
DELETE /threads/{thread_id}

GET    /threads/{thread_id}/posts
GET    /threads/{thread_id}/posts/{post_id}
POST   /threads/{thread_id}/posts
PATCH  /threads/{thread_id}/posts/{post_id}
DELETE /threads/{thread_id}/posts/{post_id}

Filtering, sorting, and pagination work on nested routes too. Post queries are automatically scoped to the parent thread.

Installation

If SQLModel, SQLAlchemy, or Tortoise is already in your project:

pip install fastapi-crud-generator

To install an ORM together with the package:

pip install "fastapi-crud-generator[sqlmodel]"
pip install "fastapi-crud-generator[sqlalchemy]"
pip install "fastapi-crud-generator[tortoise]"

Documentation

mozgsml.github.io/fastapi_crud_generator

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_crud_generator-0.1.0.tar.gz (122.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_crud_generator-0.1.0-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_crud_generator-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_crud_generator-0.1.0.tar.gz
  • Upload date:
  • Size: 122.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for fastapi_crud_generator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b67ed044456d9904b02520407c3f1d4b8f41473505bf3d9324ded9c42426f4d
MD5 115d8cfc162ddfc2c194a2967ba6f10d
BLAKE2b-256 2488461605d9566139169322ceefd3abe000fa79ede8ba476b914cfe65d09d37

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_crud_generator-0.1.0.tar.gz:

Publisher: release.yml on mozgsml/fastapi_crud_generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_crud_generator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_crud_generator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac26bd8279c054f8b041f46879f3fc0e2a7fa0887660d15b9838a6c2d3729c82
MD5 bdf469a5cfd7761117ff979012a195c8
BLAKE2b-256 b2f3f2c1355022931d7f073be51b2c2a5b21dfddb832d958d80e280dc32701ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_crud_generator-0.1.0-py3-none-any.whl:

Publisher: release.yml on mozgsml/fastapi_crud_generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page