Get Django-NonRel working with VirtualEnv

September 24, 2010   

MongoDB is getting a lot of press recently. I stumbled across a pretty interesting project called Django-nonrel. This is an attempt to build non-relational databases right into mongodb. Before you could always use a 3rd party ORM or just use pymongo right in Django but then you were missing out on all the nice apps the community has created. So the goal of this project is to make it so you can keep on using those. Now as of writing this joins are not working but from what I know they are working on it.

So lets start off by creating a virtualenv environment. I will call my project photoblog

virtualenv photoblog

Now lets activate our virtualenv

source ./photoblog/bin/activate

Now we can install Django-nonrel

wget http://bitbucket.org/wkornewald/django-nonrel/get/tip.zip
unzip tip.zip
cd django-nonrel
python setup.py build
python setup.py install

Now lets install pymongo

pip install pymongo

Now lets install the djangotoolbox

wget http://bitbucket.org/wkornewald/djangotoolbox/get/tip.zip
unzip tip.zip
cd djangotoolbox
python setup.py build
python setup.py install

Now we have to download and install the django-mongo-engine

wget --no-check-certificate https://github.com/aparo/django-mongodb-engine/zipball/master 
unzip aparo-django-mongodb-engine*
cd aparo-django-mongodb-engine*
python setup.py build
python setup.py install

So now lets create the Django project

cd ~/photoblog
django-admin.py startproject photoblog
cd photoblog

Now the only thing you really have to change to get a basic site up and running is the following file

settings.py

So edit that and find the block that looks like

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        '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.
    }
}

And you want it to look something like

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine.mongodb', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'photoblog',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '27017',                      # Set to empty string for default. Not used with sqlite3.
    }
}

Then you want to add the following to your INSTALLED_APPS

djangotoolbox

Now you can sync the db to set it all up!

cd ~/photoblog/photoblog
chmod +x manage.py
./manage.py syncdb

You are all set!



comments powered by Disqus