A Python Object to MongoDB Document and Relational Database Mapper
Simplify Query Creation
Writing query documents can get out of control. Djongo does the heavy lifting of creating query documents for you.
SQL <=> No SQL
Switch between MongoDB and other SQL databases. Limit the impact to your Models and business logic.
Security
Directly saving raw JSON
into the database is scary. Djongo secures and validates the JSON
document before saving.
Rapid Prototyping
Speed up app development and execution with schema free models. Enforce levels of schema protection with data evolution.
Djongo is a smarter approach to pymongo programming. It is an extension to the traditional Django ORM framework. It maps python objects to MongoDB documents, a technique popularly referred to as Object Document Mapping or ODM.
Constructing queries using Djongo is much easier compared to writing lengthy Pymongo query documents.
Storing raw JSON
emitted by the frontend directly into the database is scary. Djongo ensures that only clean data gets through.
You no longer need to use the shell to inspect your data. By using the Admin
package, you can access and modify data directly from the web browser. Djongo carries handy UI elements that help represent MongoDB documents on the browser.
Installation and Setup
-
Download and install the latest version of Djongo by running:
pip install djongo
-
The project directory is where all Djongo settings live. Auto generate the required files by running:
django-admin startproject mysite
-
You can replace mysite with a name of your choosing. Go into the root of mysite directory to find the
settings.py
file. Add:DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'your-db-name', } }
-
YOU ARE SET! Have fun!
Query Creation
PyMongo
self.db['entry'].aggregate(
[{
'$match': {
'author_id': {
'$ne': None,
'$exists': True
}
}
},
{
'$lookup': {
'from': 'author',
'localField': 'author_id',
'foreignField': 'id',
'as': 'author'
}
},
{
'$unwind': '$author'
},
{
'$lookup': {
'from': 'blog',
'localField': 'blog_id',
'foreignField': 'id',
'as': 'blog'
}
},
{
'$unwind': {
'path': '$blog',
'preserveNullAndEmptyArrays': True
}
},
{
'$addFields': {
'blog': {
'$ifNull': ['$blog', {
'id': None,
'title': None
}]
}
}
},
{
'$match': {
'author.name': {
'$eq': 'Paul'
}
}
},
{
'$project': {
'id': True,
'blog_id': True,
'author_id': True,
'content': True,
'blog.id': True,
'blog.title': True
}
}]
Djongo
qs = Entry.objects.filter(author__name='Paul')\
.select_related('blog')
Djongo generates complex, error free, aggregation queries automatically. It takes the relatively simple query on the right and automatically generates the pymongo query document on the left.
Security and Integrity Checks
def script_injection(value):
if value.find('<script>') != -1:
raise ValidationError(_('Script injection in %(value)s'),
params={'value': value})
class Entry(models.Model):
homepage = models.URLField(validators=[URLValidator,
script_injection])
Djongo performs checks on data fields before they are saved to the database.
Define custom validators or use builtin validators to check the data. Validation is triggered prior to writing to the database.
Running integrity checks and field value validators ensures protect from garbage data.
Rapid Prototyping
As your data evolves you may wish to enforce a structure to it. The JSONField
represents documents with no structure, while setting enforce_schema = True
in the settings.py
file enables checks to the data.
Next, the EmbeddedField
lets you describe the structure which triggers automatic validations at the application level.
Finally, you can enable schema checks at the database level. MongoDB schema documents are created inside a model
. Setting enforce_schema = True
in the settings.py
file enables schema checks on the stored collections.