GithubHelp home page GithubHelp logo

isabella232 / django-mongoengine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mongoengine/django-mongoengine

0.0 0.0 0.0 839 KB

django mongoengine integration

License: Other

Python 91.44% JavaScript 5.46% HTML 3.04% Makefile 0.06%

django-mongoengine's Introduction

Django-MongoEngine

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 2.0 and 3.0;

WARNING:

Maybe there is better option for mongo support, take a look at https://nesdis.github.io/djongo/; It's python3 only and i have not tried it yet, but looks promising.

Working / Django 2.0-3.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.

Note

SESSION_SERIALIZER is only necessary in Django>1.6 as the default serializer is based around JSON and doesn't know how to convert bson.objectid.ObjectId instances to strings.

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

django-mongoengine's People

Contributors

apolkosnik avatar dblado avatar dependabot[bot] avatar dqfort avatar erdenezul avatar erezarnon avatar farhan-cashify avatar fbuccioni avatar g3ar avatar iliyass avatar ipfans avatar jackeygao avatar jesusch avatar last-partizan avatar liwancheng91 avatar lsaint avatar martinrusev avatar milidam avatar parisk avatar rozza avatar tonymistark avatar u1735067 avatar vayel avatar zzzuzik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.