Skip to main content

Industrial Flask framework for building RESTful APIs with MongoDB

Project description

๐Ÿš€ flask-mongo-drf

PyPI version Python 3.7+ License: MIT Documentation Status

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 FilterSet with multiple lookup expressions (icontains, exact, gt, lt, etc.).
  • Flexible Pagination: Customizable pagination with configurable page sizes.
  • Serialization & Validation: Declarative field validation with Serializer classes.
  • 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:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/amazing-feature).
  3. Commit your changes (git commit -m 'Add amazing feature').
  4. Push to the branch (git push origin feature/amazing-feature).
  5. 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


Download files

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

Source Distribution

flask_mongo_drf-1.0.1.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

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

flask_mongo_drf-1.0.1-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file flask_mongo_drf-1.0.1.tar.gz.

File metadata

  • Download URL: flask_mongo_drf-1.0.1.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

Hashes for flask_mongo_drf-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0ed1bc2159e91a44b20e95f05a0bb6bf3f5327ebf0ecebcb3fb4a9fecb512881
MD5 4b1c7a93e36dfe593cdde5d980ab0566
BLAKE2b-256 07dd929d31d463b994b70f75f471e0b8e43e0fc23eaccc6f6a3fea6dafa2626e

See more details on using hashes here.

File details

Details for the file flask_mongo_drf-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_mongo_drf-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 35debd0eacda781d2f69d8b07a38bf12a57618bd9fdc5a87c7d1c4160b820889
MD5 8dc37319e565ce0357850cb468c2202f
BLAKE2b-256 64902af39e16122358611348cfe83ebef6beeefb230552dd5b14adffa6edee42

See more details on using hashes here.

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