django之多数据库的处理(day-28) - 图1

    首先,在settings配置上另一个数据库running

    1. DATABASES = {
    2. 'default': {
    3. 'ENGINE': 'django.db.backends.sqlite3',
    4. 'NAME': BASE_DIR / 'db.sqlite3',
    5. },
    6. 'running': {
    7. 'ENGINE': 'django.db.backends.mysql',
    8. 'NAME': 'running',
    9. 'USER': 'root',
    10. 'PASSWORD': '123456',
    11. 'HOST': '127.0.0.1',
    12. 'PORT': '3306',
    13. }

    接着往项目中同步表inspectdb (‘> models.py’指定创建到根目录下的文件中,前面也可以加想要的表的名字)
    python manage.py inspectdb —database=running —settings=settings.local > models.py

    然后去创建一个APP用来存放他,记得在settings中注册running
    django-admin startapp running
    然后把刚刚的models.py的内容搬到running中去
    同时,也得在admin.py中注册models

    1. from django.contrib import admin
    2. # Register your models here.
    3. from myyuque.running.models import Country, Province
    4. admin.site.register(Country)
    5. admin.site.register(Province)

    接着重点来了!
    需要配置数据库路由,base.py同目录下创建router.py

    1. # -*- coding: utf-8 -*-
    2. from django.conf import settings
    3. DATABASE_MAPPING = settings.DATABASE_APPS_MAPPING
    4. class DatabaseAppsRouter(object):
    5. """
    6. A router to control all database operations on models for different
    7. databases.
    8. In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
    9. will fallback to the `default` database.
    10. Settings example:
    11. DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
    12. """
    13. def db_for_read(self, model, **hints):
    14. """"
    15. Point all read operations to the specific database.
    16. """
    17. if model._meta.app_label in DATABASE_MAPPING:
    18. return DATABASE_MAPPING[model._meta.app_label]
    19. return 'default'
    20. def db_for_write(self, model, **hints):
    21. """
    22. Point all write operations to the specific database.
    23. """
    24. if model._meta.app_label in DATABASE_MAPPING:
    25. return DATABASE_MAPPING[model._meta.app_label]
    26. return 'default'
    27. def allow_relation(self, obj1, obj2, **hints):
    28. """
    29. Allow any relation between apps that use the same database.
    30. """
    31. # db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
    32. # db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
    33. # if db_obj1 and db_obj2:
    34. # if db_obj1 == db_obj2:
    35. # return True
    36. # else:
    37. # return False
    38. return None
    39. # for Django 1.4 - Django 1.6
    40. def allow_syncdb(self, db, model):
    41. """
    42. Make sure that apps only appear in the related database.
    43. """
    44. # if db in DATABASE_MAPPING.values():
    45. # return DATABASE_MAPPING.get(model._meta.app_label) == db
    46. # elif model._meta.app_label in DATABASE_MAPPING:
    47. # return False
    48. return None
    49. # Django 1.7 - Django 1.11
    50. def allow_migrate(self, db, app_label, model_name=None, **hints):
    51. # print db, app_label, model_name, hints
    52. if db in DATABASE_MAPPING.values():
    53. return DATABASE_MAPPING.get(app_label) == db
    54. elif app_label in DATABASE_MAPPING:
    55. return False
    56. return None

    然后在base.py 中配置

    1. # 配置多数据库路由
    2. DATABASE_ROUTERS = ['settings.router.DatabaseAppsRouter'] # 路径
    3. # 根据app名称路由指定的数据库
    4. DATABASE_APPS_MAPPING = {
    5. 'running': 'running',
    6. }

    最后在running的models.py中的每个Meta加上 app_label = ‘running’