这次给大家带来django多数据库使用步奏详解,django多数据库使用的注意事项有哪些,下面就是实战案例,一起来看一下。
1.在settings中设定database
比如要使用两个数据库:
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个数据库,别名一个为default,一个为user。数据库的别名可以任意确定。
default的别名比较特殊,一个model在路由中没有特别选择时,默认使用default数据库。
当然,default也可以设置为空:
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'
}
}
这样,因为没有了默认的数据库,就需要为所有的model,包括使用的第三方库中的model做好数据库路由选择。
2.为需要做出数据库选择的model规定app_label
class myuser(models.model):
...
class meta:
app_label = 'users'
3.写database routers
database router用来确定一个model使用哪一个数据库,主要定义以下四个方法:
db_for_read(model, **hints)
规定model使用哪一个数据库读取。
db_for_write(model, **hints)
规定model使用哪一个数据库写入。
allow_relation(obj1, obj2, **hints)
确定obj1和obj2之间是否可以产生关联, 主要用于foreign key和 many to many操作。
allow_migrate(db, app_label, model_name=none, **hints)
确定migrate操作是否可以在别名为db的数据库上运行。
一个完整的例子:
数据库设定:
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',
},
}
如果想要达到如下效果:
app_label为auth的model读写都在auth_db中完成,其余的model写入在primary中完成,读取随机在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读写都在auth_db中完成,允许有关联,migrate只在auth_db数据库中可以运行。
其余的:
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
这样读取在随机在replica1和replica2中完成,写入使用primary。
最后在settings中设定:
database_routers = ['path.to.authrouter', 'path.to.primaryreplicarouter']
就可以了。
进行migrate操作时:
$ ./manage.py migrate
$ ./manage.py migrate --database=users
migrate操作默认对default数据库进行操作,要对其它数据库进行操作,可以使用--database选项,后面为数据库的别名。
与此相应的,dbshell,dumpdata,loaddata命令都有--database选项。
也可以手动的选择路由:
查询:
>>> # 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')
移动:
>>> p = person(name='fred')
>>> p.save(using='first') # (statement 1)
>>> p.save(using='second') # (statement 2)
以上的代码会产生问题,当p在first数据库中第一次保存时,会默认生成一个主键,这样使用second数据库保存时,p已经有了主键,这个主键如果未被使用不会产生问题,但如果先前被使用了,就会覆盖原先的数据。
有两个解决方法;
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)
删除:
从哪个数据库取得的对象,从哪删除
>>> u = user.objects.using('legacy_users').get(username='fred')
>>> u.delete() # will delete from the `legacy_users` database
如果想把一个对象从legacy_users数据库转移到new_users数据库:
>>> user_obj.save(using='new_users')
>>> user_obj.delete(using='legacy_users')
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
webstorm es6怎么使用babel
使用react将组件渲染到指定dom节点
以上就是django多数据库使用步奏详解的详细内容。