A modern, high-performance Content Management System built with FastAPI, SQLModel, and Pydantic.
- API-first design: RESTful API endpoints for all resources
- Admin Dashboard: Intuitive web interface for content management
- Authentication: JWT-based authentication system
- Content Management: Articles, categories, tags, comments, and products
- Database: SQLite by default, with support for PostgreSQL, MySQL, Oracle, SQL Server and many others
- Docker Support: Easy deployment using Docker
- Responsive Design: Mobile-friendly admin interface
- FastAPI: High-performance Python web framework
- SQLModel: SQL databases in Python with type checking
- Pydantic: Data validation and settings management
- Jinja2: Template engine for the admin interface
- JWT: JSON Web Token for authentication
- Alembic: Database migration tool
fastapi-cms/
├── alembic/ # Database migrations
├── app/ # Main application
│ ├── api/ # API endpoints
│ ├── auth/ # Authentication
│ ├── models.py # SQLModel models
│ ├── config.py # Application settings
│ ├── database.py # Database connection
│ ├── main.py # Application entry point
│ ├── routers/ # Admin panel routes
│ └── utils/ # Utility functions
├── media/ # User-uploaded files
├── static/ # Static files (CSS, JS, etc.)
├── templates/ # Jinja2 templates
├── .env # Environment variables
├── .env.example # Example environment file
├── Dockerfile # Docker configuration
├── docker-entrypoint.sh # Docker entrypoint script
├── requirements.txt # Python dependencies
└── alembic.ini # Alembic configuration
- User: Authentication and authorization
- Category: Content categorization
- Article: Main content type
- Comment: User feedback on articles
- Tag: Content tagging and filtering
- Product: E-commerce product listings with external store links
- Python 3.10+
- pip (Python package manager)
-
Clone the repository:
git clone https://github.com/yourusername/fastapi-cms.git cd fastapi-cms
-
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows, use: .venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.env
file from the example:cp .env.example .env
-
Initialize the database with Alembic:
alembic revision --autogenerate -m "Initial migration" alembic upgrade head
-
Run the application:
fastapi dev app/main.py
-
Access the application:
- Admin interface: http://localhost:8000/admin
- API documentation: http://localhost:8000/docs
- Username:
admin
- Password:
admin
⚠️ Important: Change the default admin credentials in production by settingADMIN_USERNAME
,ADMIN_EMAIL
, andADMIN_PASSWORD
in your.env
file.
-
Build the Docker image:
docker build -t fastapi-cms .
-
Run the container:
docker run -p 8000:8000 -d fastapi-cms
POST /admin/login
: Login to get JWT tokenPOST /admin/logout
: Logout and invalidate token
GET /api/users/
: List all usersPOST /api/users/
: Create a new userGET /api/users/{user_id}
: Get user detailsPUT /api/users/{user_id}
: Update user detailsDELETE /api/users/{user_id}
: Delete a user
GET /api/categories/
: List all categoriesPOST /api/categories/
: Create a new categoryGET /api/categories/{category_id}
: Get category detailsPUT /api/categories/{category_id}
: Update categoryDELETE /api/categories/{category_id}
: Delete a category
GET /api/articles/
: List all articlesPOST /api/articles/
: Create a new articleGET /api/articles/{article_id}
: Get article detailsPUT /api/articles/{article_id}
: Update articleDELETE /api/articles/{article_id}
: Delete an article
The article listing endpoint (GET /api/articles/
) supports filtering, sorting, and pagination with the following query parameters:
category_id
: Filter by category IDtag_id
: Filter by tag IDauthor_id
: Filter by author IDpublished
: Filter by published status (true/false)search
: Search in title and contentsort_by
: Sort by field (created_at, updated_at)sort_order
: Sort order (asc, desc)page
: Page number (default: 1)per_page
: Items per page (default: 10, max: 100)
Example responses:
{
"items": [
{
"id": 1,
"title": "Sample Article",
"content": "Article content...",
"published": true,
"category": {...},
"author": {...},
"tags": [...],
"created_at": "2024-01-01T00:00:00",
"updated_at": "2024-01-01T00:00:00"
}
],
"total": 100,
"page": 1,
"per_page": 10,
"total_pages": 10
}
Example queries:
- Get published articles:
/api/articles?published=true
- Search articles:
/api/articles?search=python
- Filter by category and tag:
/api/articles?category_id=1&tag_id=2
- Sort by creation date:
/api/articles?sort_by=created_at&sort_order=desc
- Pagination:
/api/articles?page=2&per_page=20
- Combined filters:
/api/articles?category_id=1&published=true&search=python&sort_by=created_at&sort_order=desc&page=1&per_page=10
GET /api/comments/
: List all commentsPOST /api/comments/
: Create a new commentGET /api/comments/{comment_id}
: Get comment detailsPUT /api/comments/{comment_id}
: Update commentDELETE /api/comments/{comment_id}
: Delete a comment
GET /api/tags/
: List all tagsPOST /api/tags/
: Create a new tagGET /api/tags/{tag_id}
: Get tag detailsPUT /api/tags/{tag_id}
: Update tagDELETE /api/tags/{tag_id}
: Delete a tag
GET /api/products/
: List all productsPOST /api/products/
: Create a new productGET /api/products/{product_id}
: Get product detailsPUT /api/products/{product_id}
: Update productDELETE /api/products/{product_id}
: Delete a product
The API documentation is automatically generated using Swagger UI and available at /docs
endpoint.
FastAPI CMS supports multiple database backends through SQLAlchemy:
- SQLite: Default, no additional packages needed
- PostgreSQL/CockroachDB: Requires
psycopg2
orpsycopg2-binary
andsqlalchemy-cockroachdb
- MySQL/MariaDB: Requires
pymysql
ormysqlclient
- Oracle: Requires
cx_Oracle
- Microsoft SQL Server: Requires
pyodbc
- Firebird: Requires
fdb
- MongoDB: Requires
pymongo
- Cassandra: Requires
cassandra-driver
- IBM DB2: Requires
ibm_db_sa
- SAP HANA: Requires
hdbcli
- Snowflake: Requires
snowflake-sqlalchemy
- Amazon Redshift: Requires
redshift_connector
- Google BigQuery: Requires
pybigquery
To use a specific database, update the DATABASE_URL
in your .env
file with the appropriate connection string. See the comments in .env.example
and app/database.py
for connection string formats.
Contributions are welcome! Please feel free to submit a Pull Request.
If you find this project helpful, consider buying me a coffee!
This project is licensed under the MIT License - see the LICENSE file for details.