美文网首页
django1.9:一个app对应一个数据库

django1.9:一个app对应一个数据库

作者: testxiao111222 | 来源:发表于2019-01-25 19:00 被阅读0次

    步骤:

    1、在工程同名目录下新建一个database_route.py文件,内容如下:

    # -*- coding: utf-8 -*-
    
    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_migrate(self, db, app_label, model_name=None, **hints):
    
          # print(db, app_label, model_name, hints)
    
          if db in DATABASE_MAPPING.values():
    
             return DATABASE_MAPPING.get(app_label) == db
    
          elif app_label in DATABASE_MAPPING:
    
             return False
    
          return None
    

    2、在工程目录下的setting中增加如下内容:

    DATABASES = {
    
        'default': {
    
            'ENGINE': 'django.db.backends.mysql',
    
            'NAME': ',
    
            'USER': '',
    
            'PASSWORD': '',
    
            'HOST': '',
    
            'PORT': 3306
    
        },
    
     'db1':{
    
            'ENGINE': 'django.db.backends.mysql',
    
            'NAME': 'db1',
    
            'USER': '',
    
            'PASSWORD': '',
    
            'HOST': '',
    
            'PORT': 3306
    
        }
    
        'db2':{
    
            'ENGINE': 'django.db.backends.mysql',
    
            'NAME': 'db2',
    
            'USER': '',
    
            'PASSWORD': '',
    
            'HOST': '',
    
            'PORT': 3306
    
        }
    
    }
    
    DATABASE_ROUTERS = ['工程名.database_router.DatabaseAppsRouter']
    
    DATABASE_APPS_MAPPING = {
    
        'app1': 'db1',
    
        'app2': 'db2',
    
    }
    

    3、执行

    python manage.py makemigration app1

    python manage.py migrate mvp --database=db1

    python manage.py makemigration app2

    python manage.py migrate mvp --database=db2

    4、导出数据

    python manage.py dumpdata app1 --database=app1_db >app1_data.json(注意app1_db指的是setting中的db1标题,不是指数据库名)

    5、导入数据

    python manage.py loaddata data.json --database=app1_db

    相关文章

      网友评论

          本文标题:django1.9:一个app对应一个数据库

          本文链接:https://www.haomeiwen.com/subject/njyvjqtx.html