Multiple Database in Django

2 min read

how to combine multiple database in django

router/db_routers.py

class AuthRouter:
    route_app_labels = {'auth', 'contenttypes', 'sessions', 'admin'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'users_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'users_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'users_db'
        return None

class Issue:
    route_app_labels = {'issue'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'issue_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'issue_db'
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'issue_db'
        return None

class SecurityEmployee:
    route_app_labels = {'security'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'security_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'security_db'
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'security_db'
        return None

class Automation:
    route_app_labels = {'auto'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'auto_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'auto_db'
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'auto_db'
        return None

settings.py

DATABASE_ROUTERS = ['routers.db_routers.AuthRouter', 'routers.db_routers.Issue', 'routers.db_routers.SecurityEmployee', 'routers.db_routers.Automation',]

import environ
import os
env = environ.Env()
environ.Env.read_env()

DATABASES = {
    'default': {},
    'users_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'users_db_itsupport',
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
        'OPTIONS': {'charset': 'utf8mb4'},
    },
    'issue_db': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
        'OPTIONS': {'charset': 'utf8mb4'},
    },
    'security_db': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
        'OPTIONS': {'charset': 'utf8mb4'},
    },
    'auto_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'automation_db',
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
        'OPTIONS': {'charset': 'utf8mb4'},
    },
}

Command Migration

python manage.py makemigrations auto
python manage.py makemigrations security
python manage.py makemigrations issue

python manage.py migrate  --database=auto --fake
python manage.py migrate  --database=security --fake
python manage.py migrate  --database=issue --fake

### Create User
python manage.py migrate
python manage.py createsuperuser --database=users_db

Calling Object Database


class Device(models.Model):
    class Meta:
        db_table = 'mikrotik_device'  
        
Obj = Device.objects.using('auto_db').all()

How to Run Django on Jupyter Notebook Visual Code

previously I have struggled to debug my app when running Python app directly; I need it to run on my Jupyter Workspace easily to...
admin
27 sec read

How to secure important data with environments in Django

settings.py .Env
admin
20 sec read

How to resolve UnicodeDecodeError Django

pada settingan docker edit volume mysql config
admin
22 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *