Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

:rem:`|||:sec:|||`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

Note

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

:rem:`|||:sec:|||`Configuration

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

DATABASES = {
    '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.
    }
}

Note

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.

:rem:`|||:sec:|||`Site/Apps

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

:rem:`|||:sec:|||`Tutorial

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 ...

:html:`<hr>`

:rem:`|||:sec:|||`Copyright

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.

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.