A Django adapter to facilitate using MongoDB with MongoEngine.
Project description
Django Mongo Adapter
A lightweight adapter that bridges the gap between Django and MongoDB (via MongoEngine). It provides utilities to seamlessly integrate MongoDB documents into the Django Admin, simplifies connection management, offers a pre-configured test runner for mocking MongoDB, and includes mixins for standardized audit trails.
Features
- Automatic Connection Management: Configures
mongoengineconnections via Django settings. - Django Admin Integration: View and filter MongoDB documents in the Django Admin as if they were SQL models.
- Generic Wrappers: Utilities to make Mongo QuerySets behave like Django QuerySets (great for generic views).
- Test Runner: A drop-in
MongoMockTestRunnerthat ensures your tests never hit a real database. - Audit Mixins: Standardized
created_at,updated_at, and user tracking for Mongo documents. - Direct Imports: Import common MongoEngine components (
Document,fields,mock_mongo_connection) directly from the package.
Installation
-
Install the package:
pip install smoothglue-django-mongo-adapter
-
Add it to your
INSTALLED_APPSinsettings.py:INSTALLED_APPS = [ # ... "smoothglue.django_mongo_adapter", ]
Configuration
Define your MongoDB connection settings in settings.py. The adapter will automatically initialize the connection on startup.
MONGO_CLIENT_CONNECTION = {
"alias": "default",
"host": "mongodb://localhost:27017/my_database",
# "username": "root",
# "password": "password",
}
Usage
1. Django Admin Integration
To display a MongoDB document in the Django Admin, you need two things:
- The MongoEngine Document (your actual data).
- A Django Proxy Model (to tell Django Admin what fields to display).
models.py:
from django.db import models
from mongoengine import Document, StringField
# 1. The Mongo Document
class MyLog(Document):
message = StringField()
level = StringField()
# 2. The Django Proxy Model
class MyLogAdminView(models.Model):
id = models.UUIDField(primary_key=True)
message = models.CharField(max_length=255)
level = models.CharField(max_length=50)
class Meta:
managed = False # Crucial: tells Django not to create a table
verbose_name = "System Log"
admin.py:
Inherit from MongoModelAdmin and set the mongo_document attribute.
from django.contrib import admin
from smoothglue.django_mongo_adapter.utils import MongoModelAdmin
from .models import MyLog, MyLogAdminView
@admin.register(MyLogAdminView)
class MyLogAdmin(MongoModelAdmin):
mongo_document = MyLog
list_display = ("message", "level")
# Optional: Map Django field names to Mongo fields/paths
mongo_mapper = {
"django_field_name": "mongo.field.path"
}
2. Custom Views
If you need to pass MongoDB data to a Django Generic View (like ListView), use the AutoMongoQuerySetWrapper:
from django.views.generic import ListView
from smoothglue.django_mongo_adapter.utils import AutoMongoQuerySetWrapper
from .models import MyLog, MyLogAdminView
class LogListView(ListView):
template_name = "logs.html"
def get_queryset(self):
# Wraps the Mongo QuerySet so it behaves like a Django QuerySet
return AutoMongoQuerySetWrapper(MyLog.objects.all(), MyLogAdminView)
3. Testing
Use the provided test runner to automatically mock MongoDB connections using mongomock.
settings.py:
TEST_RUNNER = "smoothglue.django_mongo_adapter.test_runner.MongoMockTestRunner"
MONGO_DATABASES = {
"test": {"db": "mongo-test-db"}
}
4. Audit Mixins
Easily add audit fields to your Mongo documents.
from smoothglue.django_mongo_adapter import Document, fields, MongoTimeAuditDocument, MongoUserAuditDocument
class MyDocument(MongoTimeAuditDocument, MongoUserAuditDocument, Document):
name = fields.StringField()
# Automatically has:
# - created_at (datetime)
# - updated_at (datetime)
# - created_by (property, links to Django User)
# - updated_by (property, links to Django User)
To set the user:
doc = MyDocument(name="Test")
doc.created_by = request.user
doc.save()
5. Utilities
You can use mock_mongo_connection to patch MongoEngine connection in your tests without importing mongoengine or mongomock directly:
from smoothglue.django_mongo_adapter import mock_mongo_connection
@mock_mongo_connection()
class MyTest(unittest.TestCase):
# ...
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 smoothglue_django_mongo_adapter-0.0.1.tar.gz.
File metadata
- Download URL: smoothglue_django_mongo_adapter-0.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/4.18.0-553.77.1.el8_10.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98977d3221c5f6d7279202c10da7ea475c6efec46c19007a4c2265f4e536a155
|
|
| MD5 |
bd07e7c6477e2767db03edd757a9ae49
|
|
| BLAKE2b-256 |
dc0b88fb1b9b11ee0f8abbccaa4e82fca94711bece0253f354ea7e100dc825f1
|
File details
Details for the file smoothglue_django_mongo_adapter-0.0.1-py3-none-any.whl.
File metadata
- Download URL: smoothglue_django_mongo_adapter-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/4.18.0-553.77.1.el8_10.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
380d965a7872b8cb6ddf2a6b49bd866924f3558e5e80a26bda17b91ca754ad97
|
|
| MD5 |
daa681cdffcb1bbcf3b8e0914b0d18aa
|
|
| BLAKE2b-256 |
a5a2d5d7f627f7c2227b9eba733b707545ba097fd6e928bd565e36f4ca8fe2cf
|