在django里使用多个数据库

来源:互联网 发布:美国大豆数据 编辑:程序博客网 时间:2024/05/16 12:29

转载自:http://blog.csdn.net/feng88724/article/details/7177957

1.2之后,  django支持在项目中使用多个DB. 那么到底如何使用呢?

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。

01classMyAppRouter(object):
02    """A router to control all database operations on models in
03    the myapp application"""
04 
05    defdb_for_read(self, model, **hints):
06        "Point all operations on myapp models to 'other'"
07        ifmodel._meta.app_label =='myapp':
08            return'other'
09        returnNone
10 
11    defdb_for_write(self, model, **hints):
12        "Point all operations on myapp models to 'other'"
13        ifmodel._meta.app_label =='myapp':
14            return'other'
15        returnNone
16 
17    defallow_relation(self, obj1, obj2, **hints):
18        "Allow any relation if a model in myapp is involved"
19        ifobj1._meta.app_label =='myapp' or obj2._meta.app_label =='myapp':
20            returnTrue
21        returnNone
22 
23    defallow_syncdb(self, db, model):
24        "Make sure the myapp app only appears on the 'other' db"
25        ifdb =='other':
26            returnmodel._meta.app_label =='myapp'
27        elifmodel._meta.app_label =='myapp':
28            returnFalse
29        returnNone

同步数据库的时候,默认会同步到Default数据库,当然也可以通过 --database 来指定同步哪一个, 如下:

[plain] view plaincopyprint?
  1. <tt class="xref std std-djadminopt docutils literal" style="text-decoration: none; white-space: nowrap; color: rgb(35, 79, 50); margin-left: 0px; margin-right: 0px; border-bottom-width: 1px; border-bottom-color: rgb(35, 79, 50); border-bottom-style: dotted; ">$ ./manage.py syncdb  
  2. $ ./manage.py syncdb --database=users</tt>  

那么程序中如何来选择呢?

比如,下面的代码将选择default数据库

[python] view plaincopyprint?
  1. <span style="font-family:monospace;color:#234f32;">>>> # This will run on the 'default' database.  
  2. >>> Author.objects.all()  
  3.   
  4. >>> # So will this.  
  5. >>> Author.objects.using('default').all()</span>  

但是下面的代码将选择other数据库

[python] view plaincopyprint?
  1. <span style="font-family:monospace;color:#234f32;">>>> # This will run on the 'other' database.  
  2. >>> Author.objects.using('other').all()</span>  

上面是查询的情况,保存的使用也一样,也是通过using来指定,如下:

[python] view plaincopyprint?
  1. <span style="font-family:monospace;color:#234f32;">>>> my_object.save(using='legacy_users')</span>  

删除的时候

[python] view plaincopyprint?
  1. <span style="font-family:monospace;color:#234f32;">>>> u = User.objects.using('legacy_users').get(username='fred')  
  2. >>> u.delete() # will delete from the `legacy_users` database</span>  

更多内容请至此查看: https://docs.djangoproject.com/en/1.3/topics/db/multi-db/
0 0
原创粉丝点击