1. Wolfgang Scherer
  2. django-nonrel-example-setup




Author: Wolfgang Scherer

See http://django-mongodb.org/.

check.py is a working example for the tutorial setup.

:rem:`|||:sec:|||`Automatic Setup

The script setup.sh creates the virtualenv/site/app/configuration as described below.

:rem:`|||:sec:|||`Virtualenv Setup

Create a virtual env:

>>> virtualenv nonrelblog-env

To join the environment, use (in Bash):

>>> source nonrelblog-env/bin/activate


This is not necessary, when using ve.sh.

To used forked PyMongo (optional):

>>> ve.sh pip install git+file:///home/ws/project/ws-util/mongodb/pymongo/mongo-python-driver-git

Install django-nonrel

>>> ve.sh pip install hg+https://bitbucket.org/wkornewald/django-nonrel

Install djangotoolbox:

>>> ve.sh pip install hg+https://bitbucket.org/wkornewald/djangotoolbox

Install django_mongodb_engine:

You should use the latest Git revision.

>>> ve.sh pip install git+https://github.com/django-nonrel/mongodb-engine


Database setup is easy (see also the Django database setup docs):

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'django-nonrelblog.db',
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    'mongo': {
        'ENGINE': 'django_mongodb_engine',
        'NAME': 'django-nonrelblog',
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.


Using django_mongodb_engine for default database won't work for django.contrib.admin. So, it's better to use an explicit mongo database.

Django MongoDB Engine also takes into account the HOST, PORT, USER, PASSWORD and OPTIONS settings.

Possible values of OPTIONS are described in the settings reference. Done!

That’s it! You can now go straight ahead developing your Django application as you would do with any other database.


Create Django project:

>>> ve.sh django-admin.py startproject mysite

Create Django App:

>>> cd mysite
>>> ../ve.sh python manage.py startapp nonrelblog
>>> cd ..

Start Django dev server:

>>> ve.sh python mysite/manage.py runserver

Django shell:

>>> ve.sh python mysite/manage.py shell


Define Post in mysite/nonrelblog/models.py:

from django.db import models
from djangotoolbox.fields import ListField

class Post(models.Model):
    title = models.CharField()
    text = models.TextField()
    tags = ListField()
    comments = ListField()

Let’s try this out. Fire up a Django shell and add a post:

>>> from nonrelblog.models import Post
>>> post = Post.objects.using("mongo").create(
...     title='Hello MongoDB!',
...     text='Just wanted to drop a note from Django. Cya!',
...     tags=['mongodb', 'django']
... )

Surely we want to add some comments.

>>> post.comments
>>> post.comments.extend(['Great post!', 'Please, do more of these!'])
>>> post.save()

Look and see, it has actually been saved!

>>> Post.objects.using("mongo").get().comments
[u'Great post!', u'Please, do more of these!']

In the MongoDB shell, you can see how the resulting data record looks like:

  "_id" : ObjectId("..."),
  "tags" : ["mongodb", "django"],
  "text" : "Just wanted to drop a note from Django. Cya!",
  "title" : "Hello MongoDB!",
  "comments" : [
    "Great post!",
    "Please, do more of these!"

You may have noticed something’s missing from the Post class:

We have no information about the date and time our posts are created! Fixed easily ...



Copyright (C) 2012, Wolfgang Scherer, <sw@wiedenmann-seile.de>. Sponsored by Wiedenmann-Seile GmbH. See the document source for conditions of use under the GNU Free Documentation License.