Skip to main content

Django support for MongoDB via MongoEngine

Project description

OSS Lifecycle Gitter chat

THIS IS UNSTABLE PROJECT, IF YOU WANT TO USE IT - FIX WHAT YOU NEED

Right now we’re targeting to get things working on Django 3.0 and 4.0;

WARNING: This project is not in good state, and is likely to break with django updates. It’s better to use raw mongoengine.

Working / Django 3.0-4.0

  • [ok] sessions

  • [ok] models/fields, fields needs testing

  • [ok] views

  • [ok] auth

  • [?] admin - partially working, some things broken

Current status

Many parts of projects rewritten/removed; Instead of copying django code i try to subclass/reuse/even monkey-patch; Everything listed above is working; admin - just base fuctions like changelist/edit, not tested with every form type; need’s more work.

Some code just plaholder to make things work; django/forms/document_options.py - dirty hack absolutely required to get thigs work with django. It replaces mongo _meta on model/class and provide django-like interface. It get’s replaced after class creation via some metaclass magick.

Fields notes

  • mongo defaults Field(required=False), changed to django-style defaults -> Field(blank=False), and setting required = not blank in Field.__init__

TODO

Connecting

In your settings.py file, add following lines:

MONGODB_DATABASES = {
    "default": {
        "name": database_name,
        "host": database_host,
        "password": database_password,
        "username": database_user,
        "tz_aware": True, # if you using timezones in django (USE_TZ = True)
    },
}

INSTALLED_APPS += ["django_mongoengine"]

Documents

Inhherit your documents from django_mongoengine.Document, and define fields using django_mongoengine.fields.:

from django_mongoengine import Document, EmbeddedDocument, fields

class Comment(EmbeddedDocument):
    created_at = fields.DateTimeField(
        default=datetime.datetime.now, editable=False,
    )
    author = fields.StringField(verbose_name="Name", max_length=255)
    email  = fields.EmailField(verbose_name="Email")
    body = fields.StringField(verbose_name="Comment")

class Post(Document):
    created_at = fields.DateTimeField(
        default=datetime.datetime.now, editable=False,
    )
    title = fields.StringField(max_length=255)
    slug = fields.StringField(max_length=255, primary_key=True)
    comments = fields.ListField(
        fields.EmbeddedDocumentField('Comment'), blank=True,
    )

Sessions

Django allows the use of different backend stores for its sessions. MongoEngine provides a MongoDB-based session backend for Django, which allows you to use sessions in your Django application with just MongoDB. To enable the MongoEngine session backend, ensure that your settings module has 'django.contrib.sessions.middleware.SessionMiddleware' in the MIDDLEWARE_CLASSES field and 'django.contrib.sessions' in your INSTALLED_APPS. From there, all you need to do is add the following line into your settings module:

SESSION_ENGINE = 'django_mongoengine.sessions'
SESSION_SERIALIZER = 'django_mongoengine.sessions.BSONSerializer'

Django provides session cookie, which expires after `SESSION_COOKIE_AGE` seconds, but doesn’t delete cookie at sessions backend, so 'mongoengine.django.sessions' supports mongodb TTL.

How to run example app

poetry install
poetry run pip install -r example/tumblelog/requirements.txt
poetry run python example/tumblelog/manage.py runserver

How to run tests

poetry install
poetry run python -m pytest

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

django-mongoengine-0.5.5.tar.gz (177.5 kB view details)

Uploaded Source

File details

Details for the file django-mongoengine-0.5.5.tar.gz.

File metadata

  • Download URL: django-mongoengine-0.5.5.tar.gz
  • Upload date:
  • Size: 177.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for django-mongoengine-0.5.5.tar.gz
Algorithm Hash digest
SHA256 c67c21d4a6458ee6ccb985197299f4b1934c81a4b270e6d5e5069e9a675d4435
MD5 4ec2abc1a1f30b4b53684c12e6cd8200
BLAKE2b-256 164646e132cb55e17d523fc576abfdb70871b61ea05a3d31595ae6cba849c2cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page