1、 添加数据库路由分配文件
在项目文件夹里创建‘database_router’文件。将下面的代码复制到该文件里。
from Django.conf import settings
DATABASE_MAPPING = settings.DATABASE_APPS_MAPPING
class DatabaseAppsRouter(object):
"""
A router to control all database operations on models for different
databases.
In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
will fallback to the `default` database.
Settings example:
DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
"""
def db_for_read(self, model, **hints):
""""Point all read operations to the specific database."""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def db_for_write(self, model, **hints):
"""Point all write operations to the specific database."""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def allow_relation(self, obj1, obj2, **hints):
"""Allow any relation between apps that use the same database."""
db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
if db_obj1 and db_obj2:
if db_obj1 == db_obj2:
return True
else:
return False
return None
def allow_syncdb(self, db, model):
"""Make sure that apps only appear in the related database."""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(model._meta.app_label) == db
elif model._meta.app_label in DATABASE_MAPPING:
return False
return None
def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(app_label) == db
elif app_label in DATABASE_MAPPING:
return False
return None
2、在settings.py文件中配置多数据库
# Database
# https://docs.djanGoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.Mysql',
'NAME': 'django_test',
'HOST': '127.0.0.1',
'USER': 'root',
'PASSWord': '123456',
'PORT': '3306',
},
#配置第二个数据库
'test': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xsanjiaocheng',
'HOST': '127.0.0.1',
'USER': 'root',
'PASSWORD': '123456',
'PORT': '3306',
}
}
#设置数据库路由,将django_test改为你项目的名称。
DATABASE_ROUTERS = ['django_test.database_router.DatabaseAppsRouter']
#配置数据库与app的对应关系
DATABASE_APPS_MAPPING = {
# example:
# 'app_name':'database_name',
# 'app01': 'test',
'app01': 'default',
'app02': 'test',
}
3、 在对应的app里的models.py文件里正常创建数据表即可(在创建表时尽量不要使用同样的表名)
app01中的models.py:
class django_test_1(models.Model):
abc = models.CharField(max_length=20)
class Meta:
app_label='app01'
app02中的models.py:
class test_1(models.Model):
tests= models.CharField(max_length=20)
4、 生成迁移文件
和以前一样:python manage.py makemigrations
5、 迁移数据库
迁移时需指定数据库名
Python manage.py migrate database=test
如果针对已创建好的数据库创建对应的models.py文件不用生成迁移文件,直接执行“python manage.py inspectdb > app02/models.py --database=test”的命令即可。
在django 2.1.1版本中需要执行“python manage.py inspectdb --database=test > app02/models.py ”
6、操作数据库
1)手动选择数据库
django_test_1.objects.using('default').create(abc='hdajh')
2)自动选择数据库
和以前一样不加using()。
7、 配置urls.py
导入对应app的views.py的文件。最好命名个别名,或者给views.py文件重命名。
其他使用和以前一样。
0