複数のデータベースを使う
| revision-up-to: | 5613 (0.97pre SVN) |
|---|
Warning
このドキュメントは multi-db ブランチ
一般的な Django の使い方では,全てのアプリケーションを一つのデータベース接 続で扱います.とはいえ,Django はアプリケーション単位,モデル単位またはアド ホックにデータベース接続を張るような設定もサポートしています.複数データベー スのサポートはオプションです.
複数のデータベース接続を設定する
Django のデフォルトのデータベース接続は,settings モジュールの DATABASE_ENGINE, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, および DATABASE_PORT といった変数で設定します.これとは別の データベースを接続するには, OTHER_DATABASES 設定を使います. OTHER_DATABASES は辞書として設定します.辞書の各キーには接続を識別するため の名前が,値には設定の入った辞書が入ります.OTEHER_DATABASES の各エントリ (「名前付きの接続(named connection)」といいます)は,デフォルトのデータベー スオプションの DATABASE_ENGINE のような設定変数をキーに持ちます.どのキーも 省略可能で,省略した場合にはデフォルトの設定値を継承します.
例を以下に示します:
DATABASE_ENGINE = 'postgresql'
DATABASE_NAME = 'django_apps'
DATABASE_USER = 'default_user'
DATABASE_PASSWORD = 'xxx'
OTHER_DATABASES = {
'local': { 'DATABASE_ENGINE': 'sqlite3',
'DATABASE_NAME': '/tmp/cache.db' },
'public': { 'DATABASE_HOST': 'public',
'DATABASE_USER': 'public_user',
'DATABASE_PASSWORD': 'xxx' }
'private': { 'DATABASE_HOST': 'private',
'DATABASE_USER': 'private_user',
'DATABASE_PASSWORD': 'xxx' }
}
DATABASE_* 設定に加えて, OTHER_DATABASES の名前付き接続には MODELS という 省略可能な設定を入れられます. Django アプリケーションやモデルの Python パ ス名のリストを入れます.MODELS を指定すると, MODELS に挙げたアプリケーショ ンやモデルの操作には,デフォルトの接続に代わって OTHER_DATABASES で設定した 接続を使うようになります.
MODELS を加えた例を示します:
OTHER_DATABASES = {
'local': { 'DATABASE_ENGINE': 'sqlite3',
'DATABASE_NAME': '/tmp/cache.db',
# A model name: only the model ContentItem
# with the app_label myapp will use this connection
'MODELS': ['myapp.ContentItem'] },
'public': { 'DATABASE_HOST': 'public',
'DATABASE_USER': 'public_user',
'DATABASE_PASSWORD': 'xxx',
# Two models in myapp will use the connection
# named 'public', as will ALL models in
# django.contribe.comments
'MODELS': ['myapp.Blog','myapp.Article',
'django.contrib.comments' ] }
# No models or apps are configured to use the private db
'private': { 'DATABASE_HOST': 'private',
'DATABASE_USER': 'private_user',
'DATABASE_PASSWORD': 'xxx' }
}
モデルの接続にアクセスする
Each manager has a db attribute that can be used to access the model's connection. Access the db attribute of a model's manager to obtain the model's currently configured connection.
Example:
from django.db import models
class Blog(models.Model)
name = models.CharField(maxlength=50)
class Article(models.Model)
blog = models.ForeignKey(Blog)
title = models.CharField(maxlength=100)
slug = models.SlugField()
summary = models.CharField(maxlength=500)
body = models.TextField()
class ContentItem(models.Model)
slug = models.SlugField()
mimetype = models.CharField(maxlength=50)
file = models.FileField()
# Get a ConnectionInfo instance that describes the connection
article_db = Article.objects.db
# Get a connection and a cursor
connection = article_db.connection
cursor = connection.cursor()
# Get the ``quote_name`` function from the backend
qn = article_db.backend.quote_name
Ordinarily you won't have to access a model's connection directly; just use the model and manager normally and they will use the connection configured for the model.
ConnectionInfo オブジェクト
FIXME Describe the ConnectionInfo object and each of its attributes.
Access named connections directly through django.db.connections. Each entry in django.db.connections is a ConnectionInfo instance bound to the settings configured in the OTHER_DATABASES entry under the same key.
Example:
from django.db import connections private_db = connections['private'] cursor = private_db.connection.cursor()
Using transactions with other database connections
Transaction managed state applies across all connections commit/rollback apply to all connections by default but you can specify individual connections or lists or dicts of connections
Changing model connections on the fly
Here's an example of primitive mirroring:
# Read all articles from the private db
# Note that we pull the articles into a list; this is necessary
# because query sets are lazy. If we were to change the model's
# connection without copying the articles into a local list, we'd
# wind up reading from public instead of private.
Article.objects.db = connections['private']
all_articles = list(Article.objects.all())
# Save each article in the public db
Article.objects.db = connections['public']
for article in all_articles:
article.save()
Thread and request isolation
connections close after each request connection settings are thread-local