Django多數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過(guò)程詳解
有些項(xiàng)目可能涉及到使用多個(gè)數(shù)據(jù)庫(kù)的情況,方法很簡(jiǎn)單。
1.在settings中設(shè)定DATABASE
比如要使用兩個(gè)數(shù)據(jù)庫(kù):
DATABASES = {
'default': {
'NAME': 'app_data',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'postgres_user',
'PASSWORD': 's3krit'
},
'users': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
}
}
這樣就確定了2個(gè)數(shù)據(jù)庫(kù),別名一個(gè)為default,一個(gè)為user。數(shù)據(jù)庫(kù)的別名可以任意確定。
default的別名比較特殊,一個(gè)Model在路由中沒(méi)有特別選擇時(shí),默認(rèn)使用default數(shù)據(jù)庫(kù)。
當(dāng)然,default也可以設(shè)置為空:
DATABASES = {
'default': {},
'users': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'superS3cret'
},
'customers': {
'NAME': 'customer_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_cust',
'PASSWORD': 'veryPriv@ate'
}
}
這樣,因?yàn)闆](méi)有了默認(rèn)的數(shù)據(jù)庫(kù),就需要為所有的Model,包括使用的第三方庫(kù)中的Model做好數(shù)據(jù)庫(kù)路由選擇。
2.為需要做出數(shù)據(jù)庫(kù)選擇的Model規(guī)定app_label
class MyUser(models.Model):
...
class Meta:
app_label = 'users'
3.寫(xiě)Database Routers
Database Router用來(lái)確定一個(gè)Model使用哪一個(gè)數(shù)據(jù)庫(kù),主要定義以下四個(gè)方法:
db_for_read(model, **hints)
規(guī)定model使用哪一個(gè)數(shù)據(jù)庫(kù)讀取。
db_for_write(model, **hints)
規(guī)定model使用哪一個(gè)數(shù)據(jù)庫(kù)寫(xiě)入。
allow_relation(obj1, obj2, **hints)
確定obj1和obj2之間是否可以產(chǎn)生關(guān)聯(lián), 主要用于foreign key和 many to many操作。
allow_migrate(db, app_label, model_name=None, **hints)
確定migrate操作是否可以在別名為db的數(shù)據(jù)庫(kù)上運(yùn)行。
一個(gè)完整的例子:
數(shù)據(jù)庫(kù)設(shè)定:
DATABASES = {
'default': {},
'auth_db': {
'NAME': 'auth_db',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'swordfish',
},
'primary': {
'NAME': 'primary',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'spam',
},
'replica1': {
'NAME': 'replica1',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'eggs',
},
'replica2': {
'NAME': 'replica2',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'bacon',
},
}
如果想要達(dá)到如下效果:
app_label為auth的Model讀寫(xiě)都在auth_db中完成,其余的Model寫(xiě)入在primary中完成,讀取隨機(jī)在replica1和replica2中完成。
auth:
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
if obj1._meta.app_label == 'auth' or \
obj2._meta.app_label == 'auth':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'auth':
return db == 'auth_db'
return None
這樣app_label為auth的Model讀寫(xiě)都在auth_db中完成,允許有關(guān)聯(lián),migrate只在auth_db數(shù)據(jù)庫(kù)中可以運(yùn)行。
其余的:
import random
class PrimaryReplicaRouter(object):
def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen replica.
"""
return random.choice(['replica1', 'replica2'])
def db_for_write(self, model, **hints):
"""
Writes always go to primary.
"""
return 'primary'
def allow_relation(self, obj1, obj2, **hints):
"""
Relations between objects are allowed if both objects are
in the primary/replica pool.
"""
db_list = ('primary', 'replica1', 'replica2')
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
All non-auth models end up in this pool.
"""
return True
這樣讀取在隨機(jī)在replica1和replica2中完成,寫(xiě)入使用primary。
最后在settings中設(shè)定:
DATABASE_ROUTERS = ['path.to.AuthRouter', 'path.to.PrimaryReplicaRouter']
就可以了。
進(jìn)行migrate操作時(shí):
$ ./manage.py migrate $ ./manage.py migrate --database=users
migrate操作默認(rèn)對(duì)default數(shù)據(jù)庫(kù)進(jìn)行操作,要對(duì)其它數(shù)據(jù)庫(kù)進(jìn)行操作,可以使用--database選項(xiàng),后面為數(shù)據(jù)庫(kù)的別名。
與此相應(yīng)的,dbshell,dumpdata,loaddata命令都有--database選項(xiàng)。
也可以手動(dòng)的選擇路由:
查詢:
>>> # This will run on the 'default' database.
>>> Author.objects.all()
>>> # So will this.
>>> Author.objects.using('default').all()
>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()
保存:
>>> my_object.save(using='legacy_users')
移動(dòng):
>>> p = Person(name='Fred') >>> p.save(using='first') # (statement 1) >>> p.save(using='second') # (statement 2)
以上的代碼會(huì)產(chǎn)生問(wèn)題,當(dāng)p在first數(shù)據(jù)庫(kù)中第一次保存時(shí),會(huì)默認(rèn)生成一個(gè)主鍵,這樣使用second數(shù)據(jù)庫(kù)保存時(shí),p已經(jīng)有了主鍵,這個(gè)主鍵如果未被使用不會(huì)產(chǎn)生問(wèn)題,但如果先前被使用了,就會(huì)覆蓋原先的數(shù)據(jù)。
有兩個(gè)解決方法;
1.保存前清除主鍵:
>>> p = Person(name='Fred') >>> p.save(using='first') >>> p.pk = None # Clear the primary key. >>> p.save(using='second') # Write a completely new object.
2.使用force_insert
>>> p = Person(name='Fred') >>> p.save(using='first') >>> p.save(using='second', force_insert=True)
刪除:
從哪個(gè)數(shù)據(jù)庫(kù)取得的對(duì)象,從哪刪除
>>> u = User.objects.using('legacy_users').get(username='fred')
>>> u.delete() # will delete from the `legacy_users` database
如果想把一個(gè)對(duì)象從legacy_users數(shù)據(jù)庫(kù)轉(zhuǎn)移到new_users數(shù)據(jù)庫(kù):
>>> user_obj.save(using='new_users') >>> user_obj.delete(using='legacy_users')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解多線程Django程序耗盡數(shù)據(jù)庫(kù)連接的問(wèn)題
- Django使用多數(shù)據(jù)庫(kù)的方法
- Django中數(shù)據(jù)庫(kù)的數(shù)據(jù)關(guān)系:一對(duì)一,一對(duì)多,多對(duì)多
- django 多數(shù)據(jù)庫(kù)配置教程
- Django app配置多個(gè)數(shù)據(jù)庫(kù)代碼實(shí)例
- Django多數(shù)據(jù)庫(kù)配置及逆向生成model教程
- django 多數(shù)據(jù)庫(kù)及分庫(kù)實(shí)現(xiàn)方式
- django 鏈接多個(gè)數(shù)據(jù)庫(kù) 并使用原生sql實(shí)現(xiàn)
- Django多數(shù)據(jù)庫(kù)聯(lián)用實(shí)現(xiàn)方法解析
- django使用多個(gè)數(shù)據(jù)庫(kù)的方法實(shí)例
相關(guān)文章
深入學(xué)習(xí)Python可變與不可變對(duì)象操作實(shí)例
Python中的數(shù)據(jù)類(lèi)型可以分為可變對(duì)象和不可變對(duì)象,了解它們之間的區(qū)別對(duì)于編寫(xiě)高效的Python代碼至關(guān)重要,本文將詳細(xì)介紹可變對(duì)象和不可變對(duì)象的概念,以及如何正確地使用它們來(lái)提高代碼的性能和可讀性2023-12-12
利用PyCharm Profile分析異步爬蟲(chóng)效率詳解
這篇文章主要給大家介紹了關(guān)于如何利用PyCharm Profile分析異步爬蟲(chóng)效率的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PyCharm具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python灰度變換中的分段線性函數(shù)專(zhuān)項(xiàng)分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變?cè)磮D像中每個(gè)像素灰度值的方法。目的是改善畫(huà)質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10
Python程序設(shè)計(jì)入門(mén)(4)模塊和包
Python語(yǔ)言功能非常強(qiáng)大,除了類(lèi)之外,還有模塊和包的概念,這有點(diǎn)像perl,本文主要介紹了包和模塊,需要的朋友可以參考下2014-06-06
Python實(shí)現(xiàn)遞歸遍歷文件夾并刪除文件
本文給大家匯總了3個(gè)Python實(shí)現(xiàn)遍歷文件夾并刪除的代碼,主要是給大家分享下這3種方法的實(shí)現(xiàn)思路,有需要的小伙伴可以參考下2016-04-04
python獲取指定時(shí)間段內(nèi)特定規(guī)律的日期列表
這篇文章主要介紹了python獲取指定時(shí)間段內(nèi)特定規(guī)律的日期列表,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Pyramid Mako模板引入helper對(duì)象的步驟方法
ylons中的mako模板,默認(rèn)會(huì)引入一個(gè)helper對(duì)象,我們可以在里面擴(kuò)展方法,應(yīng)對(duì)在模板輸出時(shí)候會(huì)常用的操作,那么在Pyramid中如何默認(rèn)引入同樣的輔助類(lèi)到模板中2013-11-11
初學(xué)python數(shù)學(xué)建模之?dāng)?shù)據(jù)導(dǎo)入(小白篇)
本篇文章是小白篇初學(xué)python的同學(xué)可以來(lái)共同學(xué)習(xí)了,本篇文章主要講解了python數(shù)學(xué)建模過(guò)程中的第一步數(shù)據(jù)導(dǎo)入,數(shù)據(jù)導(dǎo)入是所有數(shù)模編程的第一步,比你想象的更重要2021-08-08
Python二維數(shù)組不同初始化方式的差異說(shuō)明
這篇文章主要介紹了Python二維數(shù)組不同初始化方式的差異說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

