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
- COMPUTER DATABASES AND ABSTRACTING SERVICES 2Database
- COMP4039 DIS Databases InterfacesDatabase
- Centaur: A Framework for Hybrid CPU-FPGA DatabasesFrameworkFPGADatabase
- Level Up
- Oracle 12.2 How to Generate AWRs in Active Data Guard Standby DatabasesOracleDatabase
- 記一次kafka的high level和low levelKafka
- natas(level0-level14)通關詳細指南(一)
- CSS LEVEL4CSS
- E. Level Up
- E - Level K Palindrome
- HCNP Routing&Switching之BFD
- HCNA Routing&Switching之GVRPVR
- HCNP Routing&Switching之MSTP
- HCNP Routing&Switching之RSTP
- AWS Switching to an IAM role (AWS CLI)
- HCNP Routing&Switching之MUX VLANUX
- HCNP Routing&Switching之路由引入路由
- HCNP Routing&Switching之Super VLAN
- HCNP Routing&Switching之代理ARP
- [AlwaysOn2017] AlwaysOn的DMV和DMF -sys.availability_databases_clusterAIDatabase
- BUU_jarvisoj_level0JAR
- RuneScape - To verify your level of combatBAT
- [LintCode] Binary Tree Level Order
- FUNBOX-5: NEXT LEVEL
- HCNP Routing&Switching之RSTP保護
- HCNA Routing&Switching之DHCP服務
- HCNA Routing&Switching之VLAN間路由路由
- HCNA Routing&Switching之STP基礎
- HCNA Routing&Switching之PPPoE協議協議
- HCNP Routing&Switching之BGP路由控制路由
- HCNP Routing&Switching之鏈路聚合
- HCNP Routing&Switching之VRRP基礎VR
- HCNP Routing&Switching之埠隔離
- Binary-tree-level-order-traversal
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- 電平(level)&脈衝(pulse)
- [20220124]index split level.txtIndex