Johntron

Your friendly automaton explores technology



Multiple Databases in Django 1.2


Multi-database Django

Django 1.2 will contain support for multiple databases, according to Simon Willison (@simonw). Good news for people trying to scale their web application or building intranet services.

This addition means you will be able to do the following:

# In settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'data.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
    'production': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'multidb_demo_production',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
>>> from blog.models import Entry
>>> Entry.objects.all()
[]
>>> Entry.objects.using('default').all()
[]
>>> Entry.objects.using('production').all()
[, ]
>>> e = Entry.objects.using('production')[0]
>>> e._state.__dict__
{'db': 'production'}
>>> e.save(using = 'default') # WARNING: Over-writes SQLite item with same pk
>>> e


Tags: ,

blog comments powered by Disqus