1. 修改 settings.py
01DATABASES = {
02'default': {
03'NAME': 'app_data',
04'ENGINE': 'django.db.backends.postgresql_psycopg2',
05'USER': 'postgres_user',
06'PASSWORD': 's3krit'
07},
08'users': {
09'NAME': 'user_data',
10'ENGINE': 'django.db.backends.mysql',
11'USER': 'mysql_user',
12'PASSWORD': 'priv4te'
13}
14}
15
16DATABASE_ROUTERS = ['path.to.MyAppRouter']
2. 实现自己的DB routers,这里决定了每个程序使用的是哪个DB。
01class MyAppRouter(object):
02"""A router to control all database operations on models in
03the myapp application"""
04
05def db_for_read(self, model, **hints):
06"Point all operations on myapp models to 'other'"
07if model._meta.app_label == 'myapp':
08return 'other'
09return None
10
11def db_for_write(self, model, **hints):
12"Point all operations on myapp models to 'other'"
13if model._meta.app_label == 'myapp':
14return 'other'
15return None
16
17def allow_relation(self, obj1, obj2, **hints):
18"Allow any relation if a model in myapp is involved"
19if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
20return True
21return None
22
23def allow_syncdb(self, db, model):
24"Make sure the myapp app only appears on the 'other' db"
25if db == 'other':
26return model._meta.app_label == 'myapp'
27elif model._meta.app_label == 'myapp':
28return False
29return None
同步数据库的时候,默认会同步到Default数据库,当然也可以通过 --database 来指定同步哪一个, 如下:
[plain] view plain copy print?
<tt class="xref std std-djadminopt docutils literal" style="text-decoration: nonewhite-space: nowrapcolor: rgb(35, 79, 50)margin-left: 0pxmargin-right: 0pxborder-bottom-width: 1pxborder-bottom-color: rgb(35, 79, 50)border-bottom-style: dotted">$ ./manage.py syncdb
$ ./manage.py syncdb --database=users</tt>
那么程序中如何来选择呢?
比如,下面的代码将选择default数据库
[python] view plain copy print?
<span style="font-family:monospacecolor:#234f32">>>># This will run on the 'default' database.
>>>Author.objects.all()
>>># So will this.
>>>Author.objects.using('default').all()</span>
但是下面的代码将选择other数据库
[python] view plain copy print?
<span style="font-family:monospacecolor:#234f32">>>># This will run on the 'other' database.
>>>Author.objects.using('other').all()</span>
上面是查询的情况,保存的使用也一样,也是通过using来指定,如下:
[python] view plain copy print?
<span style="font-family:monospacecolor:#234f32">>>>my_object.save(using='legacy_users')</span>
删除的时候
[python] view plain copy print?
<span style="font-family:monospacecolor:#234f32">>>>u = User.objects.using('legacy_users').get(username='fred')
>>>u.delete() # will delete from the `legacy_users` database</span>
.多个数据库联用时数据导入导出使用的时候和一个数据库的区别是:
如果不是defalut(默认数据库)要在命令后边加 --database=数据库对应的settings.py中的名称 如: --database=db1 或 --database=db2
数据库同步(创建表)
python manage.py syncdb #同步默认的数据库,和原来的没有区别
#同步数据库 db1 (注意:不是数据库名是db1,是settings.py中的那个db1,不过你可以使这两个名称相同,容易使用)
python manage.py syncdb --database=db1
数据导出
python manage.py dumpdata app1 --database=db1 >app1_fixture.json
python manage.py dumpdata app2 --database=db2 >app2_fixture.json
python manage.py dumpdata auth >auth_fixture.json
数据库导入
python manage.py loaddata app1_fixture.json --database=db1
python manage.py loaddata app2_fixture.json --database=db2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)