This project is a dynamic and fully reusable CRUD framework built with FastAPI, SQLAlchemy, and PostgreSQL. The goal of this project is to allow developers to quickly create CRUD APIs for any model with minimal effort. You only define the model's fields once, and the system automatically generates:
- SQLAlchemy database models
- Pydantic schemas for request/response validation
- CRUD operations (create, read, update, delete)
- FastAPI routers for endpoints
The architecture is designed to be extensible, so adding new fields or models requires minimal changes.
- Dynamic CRUD for multiple models
- PostgreSQL integration
- Fully type-annotated Pydantic models
- Automatic schema generation from a single source of truth
- Swagger UI available at
/docsfor API testing
fastapi_project/
βββ app/
β βββ main.py # FastAPI app entry point
β βββ core/
β β βββ config.py # Configuration
β βββ db/
β β βββ base.py # SQLAlchemy Base
β β βββ session.py # DB session
β βββ models/
β β βββ user.py # Dynamic User model
β β βββ product.py # Dynamic Product model
β β βββ fields.py # Source of truth for all model fields
β βββ schemas/
β β βββ dynamic.py # Dynamic Pydantic schemas
β βββ crud/
β β βββ base.py # Generic CRUD class
β β βββ user.py # User CRUD
β β βββ product.py # Product CRUD
β βββ api/
β βββ user.py # User API endpoints
β βββ product.py # Product API endpoints
βββ requirements.txt # Python dependencies
βββ README.md
- Clone the repository:
git clone <repo_url>
cd fastapi_project- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windows- Install dependencies:
pip install -r requirements.txt-
Configure PostgreSQL in
app/core/config.py(or use.envvariables if implemented). -
Run the application:
uvicorn app.main:app --reload- Open your browser to http://127.0.0.1:8000/docs to explore the Swagger UI.
The project comes with two default models:
- User:
name,email - Product:
name,description,price,color
CRUD endpoints are automatically available:
POST /usersβ create a new userGET /usersβ get all usersGET /users/{id}β get a single userPUT /users/{id}β update userDELETE /users/{id}β delete user
All model fields are defined in one place:
app/models/fields.pyExample for Product:
PRODUCT_FIELDS = {
"name": {"type": str, "nullable": False},
"description": {"type": str, "nullable": True},
"price": {"type": float, "nullable": False},
"color": {"type": str, "nullable": True}, # Add new fields here
}Steps to add or change a field:
- Open
app/models/fields.py. - Add a new key for your field with its type and nullable property.
- Save the file.
- Restart the FastAPI server.
The new field will automatically be included in:
- SQLAlchemy model
- Pydantic schemas (Create, Read, Update)
- CRUD operations
- API endpoints
No need to touch CRUD classes or Router filesβeverything is fully dynamic.
- Field Definition:
fields.pycontains the single source of truth for fields. - Dynamic Models:
models/user.pyandmodels/product.pyusetype()to create SQLAlchemy models based on these fields. - Dynamic Schemas:
schemas/dynamic.pybuilds Pydantic models usingAnnotatedandField. - Generic CRUD:
crud/base.pyhandles all CRUD operations for any model. - Routers:
api/user.pyandapi/product.pyuse the dynamic CRUD and schemas to expose API endpoints.
This ensures that adding a new field or even a new model requires minimal code changes and everything works out of the box.