Industrial Flask framework for building RESTful APIs with MongoDB
Project description
๐ flask-mongo-drf
An industrial-grade Flask framework for building RESTful APIs with MongoDB, featuring automatic Swagger documentation, dependency injection, and production-ready patterns.
๐ Key Features
- Generic ViewSets: Rapidly build CRUD APIs with minimal boilerplate code.
- Dependency Injection: Decoupled Model and Database layers for superior testability.
- Auto-Generated Swagger: Dynamically generate OpenAPI documentation from your code.
- Multi-Database Support: Seamlessly manage multiple MongoDB connections.
- Advanced Filtering: Built-in
FilterSetwith multiple lookup expressions (icontains,exact,gt,lt, etc.). - Flexible Pagination: Customizable pagination with configurable page sizes.
- Serialization & Validation: Declarative field validation with
Serializerclasses. - Error Handling: Unified exception handling and standardized JSON responses.
๐ฆ Installation
Install from PyPI:
pip install flask-mongo-drf
Or install from source:
git clone https://github.com/BioDataMiner/flask-mongo-drf.git
cd flask-mongo-drf
pip install -e .
๐ Quick Start
1. Define Your Model
from flask_mongo_drf import MongoBaseModel
from flask_mongo_drf.contrib import MongoDBManager
class UserModel(MongoBaseModel):
def __init__(self):
collection = MongoDBManager.get_collection(
collection_name="users",
client_name="default"
)
super().__init__(collection=collection)
2. Create a Serializer
from flask_mongo_drf import Serializer
class UserSerializer(Serializer):
username = CharField(required=True, max_length=100)
email = CharField(required=True)
age = IntegerField(required=False)
3. Build a ViewSet
from flask_mongo_drf import MongoModelViewSet
class UserViewSet(MongoModelViewSet):
model_class = UserModel
serializer_class = UserSerializer
filterset_class = UserFilterSet
pagination_class = MongoPagination
4. Register Routes
from flask import Flask, Blueprint
app = Flask(__name__)
api_bp = Blueprint('api', __name__)
UserViewSet.register_routes(api_bp, url_prefix='users')
app.register_blueprint(api_bp, url_prefix='/api/v1')
if __name__ == '__main__':
app.run(debug=True)
5. Access Swagger UI
Visit http://localhost:5000/apidocs/ to explore your auto-generated API documentation.
๐ Documentation
Full documentation is available at ReadTheDocs.
Core Modules
| Module | Purpose |
|---|---|
mongo_models.py |
Base model class with CRUD operations |
mongo_viewsets.py |
Generic ViewSet for rapid API development |
mongo_serializers.py |
Field validation and data serialization |
mongo_filters.py |
Query filtering with multiple lookup types |
mongo_paginations.py |
Flexible pagination implementation |
mongo_responses.py |
Standardized JSON response formatting |
mongo_swagger.py |
Auto-generated Swagger documentation |
mongo_decorators.py |
Error handling and middleware decorators |
mongo_exceptions.py |
Custom exception classes |
contrib/mongodb_manager.py |
Multi-database connection management |
๐งช Testing
Run the test suite:
pytest tests/ -v
Run with coverage:
pytest tests/ --cov=flask_mongo_drf --cov-report=html
๐ Example Project
A complete example project is available in the examples/ directory:
cd examples/monitor
pip install -r requirements.txt
python manage.py
Then visit http://localhost:5050/apidocs/ to see the example API.
๐ ๏ธ Architecture
Dependency Injection Pattern
The framework uses constructor-based dependency injection to decouple the Model layer from database connection logic:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Flask Application โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ MongoDBManager โ โ
โ โ (Connection Factory) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Collection Instance โ โ
โ โ (PyMongo) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ MongoBaseModel โ โ
โ โ (Business Logic) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ MongoModelViewSet โ โ
โ โ (REST Endpoints) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ง Configuration
Multi-Database Setup
from flask_mongo_drf.contrib import MongoDBManager, init_mongodb
# Register multiple MongoDB clients
MongoDBManager.register_client(
name="primary",
uri="mongodb://user:pass@localhost:27017/db1",
default_db="db1"
)
MongoDBManager.register_client(
name="secondary",
uri="mongodb://user:pass@localhost:27017/db2",
default_db="db2"
)
# Initialize with Flask app
init_mongodb(app)
๐ค Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m 'Add amazing feature'). - Push to the branch (
git push origin feature/amazing-feature). - Open a Pull Request.
Please ensure all tests pass and add new tests for your changes.
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
๐ Acknowledgments
- Inspired by Django REST Framework's clean API design.
- Built on top of Flask and PyMongo.
- Thanks to the open-source community for feedback and contributions.
๐ Support
For issues, questions, or suggestions, please open an issue on GitHub.
Made with โค๏ธ by the flask-mongo-drf
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_mongo_drf-1.0.0.tar.gz.
File metadata
- Download URL: flask_mongo_drf-1.0.0.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
080bf07dc630bb4189f744d86dc744fab3467627f7619399a661fe0a3f695619
|
|
| MD5 |
4dd407ba0b6e78c89577905e7ea4c056
|
|
| BLAKE2b-256 |
3827c26ec27da0015db0c28484c6422c33a9963bf2f220cda04214c5f39bfffb
|
File details
Details for the file flask_mongo_drf-1.0.0-py3-none-any.whl.
File metadata
- Download URL: flask_mongo_drf-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75458ac5558da5cea18ba02b4a4f74f5f5eee00e14ff943e474d0e543ad0a710
|
|
| MD5 |
0108c1335c9a1c0dcc46f5870a6c9cd5
|
|
| BLAKE2b-256 |
fcecff82d2d1645ab88851efa6101dcb86e11768e71d779e5204e11fcad9d86a
|