Skip to main content

Django MongoEngine

This package, unlike Django-MongoEngine or Djongo, doesn't aim on changing SQL role in Django. Writing backend for NoSQL database is not trivial. Here MongoDB is used as helper database, to increase performance.

Installation

To install the package by pip run following command

# From Github
$ pip install git+https://github.com/giorgi94/djmongoengine.git

Usage

To start using the package, add djmongoengine to INSTALLED_APPS and define MONGODB_DATABASES

# settings.py

INSTALLED_APPS = [
    ...
    djmongoengine
    ...
]

MONGODB_DATABASES = {
    "default": {
        "name": "dbname",
        "host": '127.0.0.1',
        "port": 27017,
        "tz_aware": True,
        # "username": "user",
        # "password": "pass"
    },
}

Package provides mixin for document schema and connector

# models.py

class DocumentMixin:
    @classmethod
    def UpdateOne(cls, instance):
        # updates document in MongoDB, which corresponeds to django instance

    @classmethod
    def from_instance(cls, instance):
        # create mongoengine document based on django instance

We assume that django instance.id is stored in ID, since id returns _id from mongodb. In the example, we provide basic example to define mongoengine schema, based on django models

# myapp/models.py

class Category(models.Model):
    title = models.CharField(max_length=50, unique=True)
    alias = models.CharField(max_length=50)

# myapp/mongo.py

class Category(EmbeddedDocument):

    ID = fields.IntField()

    title = fields.StringField()
    alias = fields.StringField()

    @classmethod
    def from_instance(cls, instance):

        return cls(ID=instance.id, title=instance.title, alias=instance.alias)

In signals.py and receivers.py are defined functions and signals to sync changes in django to mongodb.

GraphQL

GraphQL is not directly connected with the package, but documentation for graphene is not beginner friendly. There are graphene-django and flask-graphql, but it doesn't allow to explore full potential of graphql and mongodb, so we use graphene-mongo for more diversity. Query is defined in

# myapp/query.py


class Query(graphene.ObjectType):
    articles = graphene.List(ArticleQuery, find=FindInput())

    def resolve_articles(self, info, **kwargs):
        find = kwargs.get("find")

        normilize_find(find)

        if find is not None:
            return Article.objects(**find)
        return Article.objects()


schema = graphene.Schema(query=Query)

# myapp/views.py

from .query import schema


def graphql_view(request):
    ...

    result = schema.execute(query)

    if result.errors:
        return JsonResponse({"errors": str(result.errors)})

    return JsonResponse({"query": result.data})

Metadata

Release files for djmongoengine 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djmongoengine 1.0
File Size Uploaded
djmongoengine-1.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djmongoengine 1.0
File Interpreter ABI Platform
djmongoengine-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / djmongoengine-1.0.tar.gz

Download URL djmongoengine-1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0945761b1df765142b5f5c8b9bde0b5415ef2c041a94db53a8ffd6861ceec828
BLAKE2b-256 checksum
How to use checksums
d69d5aa54ed6db1f3bfd7aa2fe1e7992b9662aba7f55a8de6b85ec47ac4f6085
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.6.8

Release files / djmongoengine-1.0-py3-none-any.whl

Download URL djmongoengine-1.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5bf2f35a02da8a8ee0c5c15af839f62241f28f55c5048a86ed8b40744a31e9e
BLAKE2b-256 checksum
How to use checksums
1ff72bce53b8730b771e63a744ff10cdd093b8605866a9957ed9b8c2aa58b4b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page