Django Switching Databases on Per-View Level
Django has a "multiple databases" feature that lets you read/write data from more than just the default database. I'm going to show you how to switch databases on a per-view level, rather than per-query with objects.using(), or per-server by changing DATABASE_ROUTERS.
First, some background. Let's look at how regular database routing works. It all starts with DATABASES in settings.py.
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myapp',
'USER': 'postgres',
'PASSWORD': 'password',
},
'standby': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myapp',
'USER': 'postgres',
'PASSWORD': 'password',
}
}Then, you configure a database router. Here is an example that reads from one database and writes to another. Maybe standby is a read-replica of default.
"""A router to control all database operations on models in
the website application"""
def db_for_read(self, model, **hints):
return "standby"
def db_for_write(self, model, **hints):
return "default"
def allow_relation(self, obj1, obj2, **hints):
return True
def allow_syncdb(self, db, model):
return None
First, some background. Let's look at how regular database routing works. It all starts with DATABASES in settings.py.
CODE:
DATABASES = {'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myapp',
'USER': 'postgres',
'PASSWORD': 'password',
},
'standby': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myapp',
'USER': 'postgres',
'PASSWORD': 'password',
}
}Then, you configure a database router. Here is an example that reads from one database and writes to another. Maybe standby is a read-replica of default.
CODE:
class StandbyRouter(object):"""A router to control all database operations on models in
the website application"""
def db_for_read(self, model, **hints):
return "standby"
def db_for_write(self, model, **hints):
return "default"
def allow_relation(self, obj1, obj2, **hints):
return True
def allow_syncdb(self, db, model):
return None
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733300/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Django Mysql SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTEDDjangoMySqlSessionMIT
- Time Series DatabasesDatabase
- Introduction to Databases and MySQLDatabaseMySql
- NoSQL Databases - Origin and FundamentalsSQLDatabase
- Transport of Tablespaces Between Databases (59)Database
- Databases are hammers; MapReduce is a screwdriver.Database
- Rowids in Non-Oracle DatabasesOracleDatabase
- COMPUTER DATABASES AND ABSTRACTING SERVICES 2Database
- Connections to NOMOUNT/MOUNTED or RESTRICTED Databases FailRESTDatabaseAI
- Level Up
- Centaur: A Framework for Hybrid CPU-FPGA DatabasesFrameworkFPGADatabase
- clone oracle 12c pluggable databasesOracleDatabase
- RMAN backup validate database on databases in noarchivelog modeDatabaseHive
- AWS Switching to an IAM role (AWS CLI)
- A summery after switching my major
- 記一次kafka的high level和low levelKafka
- natas(level0-level14)通關詳細指南(一)
- transaction-level or statement-level consistency; check constraintsAI
- CSS LEVEL4CSS
- Oracle level偽列Oracle
- Oracle statistic_levelOracle
- E. Level Up
- MySQL 5.1 執行show databases沒有mysql庫MySqlDatabase
- Post Installation for Databases Created after Installation of 10.2.0.4.4Database
- 值得期待的NoSQL新書《Seven Databases in Seven Weeks》SQL新書Database
- HCNP Routing&Switching之RSTP
- HCNP Routing&Switching之BFD
- HCNP Routing&Switching之MSTP
- Oracle 12.2 How to Generate AWRs in Active Data Guard Standby DatabasesOracleDatabase
- Lode Runner 2 Credits Level
- HCNP Routing&Switching之MUX VLANUX
- HCNP Routing&Switching之Super VLAN
- HCNA Routing&Switching之GVRPVR
- HCNP Routing&Switching之路由引入路由
- Django(2) - Django模板Django
- GHD Hair Straighteners a low levelAI
- 【SQL 學習】LEVEL 偽列SQL
- XMLHttpRequest Level 2 使用指南XMLHTTP