FastAPI RESTful
FastAPI-RESTful provides helper utilities to streamline FastAPI development, particularly for building RESTful APIs using class-based views (CBV) and resource inferring routers. It aims to reduce boilerplate when defining API endpoints and integrates well with common patterns like SQLAlchemy sessions. The current version is 0.6.0, and it has a moderate release cadence, with minor versions released every few months, often incorporating dependency updates and bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'sqlalchemy'
cause Attempting to use SQLAlchemy-related features (e.g., database session management) without installing the `[session]` extra.fixInstall `fastapi-restful` with the session extra: `pip install fastapi-restful[session]`. -
pydantic.v1.error_wrappers.ValidationError
cause This error or similar Pydantic V2 validation errors can occur if you've upgraded `fastapi-restful` to version `0.5.0` or later, but your Pydantic models are still written for Pydantic V1.fixMigrate your Pydantic models and validation logic to be compatible with Pydantic V2. This often involves changes to how fields are defined, default values, and custom validators. -
AttributeError: 'Session' object has no attribute 'query'
cause This typically indicates that you are attempting to use SQLAlchemy 1.x API methods (like `session.query()`) with SQLAlchemy 2.0, which is mandated by `fastapi-restful` versions `>=0.5.0` when the `[session]` extra is installed.fixUpdate your SQLAlchemy code to use the SQLAlchemy 2.0 API, primarily using `select()` constructs with `session.execute()` instead of `session.query()`. -
ERROR: Package 'fastapi-restful' requires a different Python: ...
cause Your current Python version does not meet the minimum requirements of the `fastapi-restful` package (e.g., Python < 3.7).fixUpgrade your Python environment to version 3.7 or newer to satisfy the package's requirements.
Warnings
- breaking Version 0.5.0 introduced compatibility with Pydantic V2 and SQLAlchemy 2.0. If you are upgrading from an older version and use Pydantic or SQLAlchemy, you will likely encounter breaking changes.
- breaking Support for older Python versions was dropped. Versions 0.4.2 and later require Python 3.6.2+, and the project currently officially supports Python 3.7+.
- gotcha Prior to version 0.4.5, `fastapi-restful` used to pin the `FastAPI` dependency more strictly. This could lead to dependency conflicts if your project relied on a different `FastAPI` version.
- gotcha SQLAlchemy support (e.g., session management) requires installing the `[session]` extra. Without it, you'll encounter `ModuleNotFoundError` if you try to use related features.
Install
-
pip install fastapi-restful -
pip install fastapi-restful[session]
Imports
- Resource
from fastapi_restful.resource import Resource
- cbv
from fastapi_restful.cbv import cbv
- InferringRouter
from fastapi_restful.inferring_router import InferringRouter
- Api
from fastapi_restful.app import Api
from fastapi_restful.api import Api
Quickstart
from fastapi import FastAPI
from fastapi_restful.resource import Resource
from fastapi_restful.cbv import cbv
from fastapi_restful.inferring_router import InferringRouter
app = FastAPI()
router = InferringRouter()
@cbv(router)
class HelloWorldResource(Resource):
def get_hello(self):
return {"message": "Hello World from CBV!"}
app.include_router(router)
# To run this:
# 1. Save as main.py
# 2. uvicorn main:app --reload
# 3. Access http://127.0.0.1:8000/hello