Skip to content

Instantly share code, notes, and snippets.

@pvergain
Last active November 20, 2017 13:05
Show Gist options
  • Save pvergain/5a7466e7397bdbf235ab6f914838544e to your computer and use it in GitHub Desktop.
Save pvergain/5a7466e7397bdbf235ab6f914838544e to your computer and use it in GitHub Desktop.
Django multiple databases access example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# common.db_id3web.py
#
from __future__ import unicode_literals
import django.db.models as models
class Id3webAdminMixin(object):
# https://docs.djangoproject.com/en/dev/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface
using = 'db_id3web'
def save_model(self, request, obj, form, change):
# Tell Django to save objects to the 'other' database.
obj.save(using=self.using)
def delete_model(self, request, obj):
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
def get_queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(MultiDBModelAdmin, self).get_queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(MultiDBModelAdmin, self).formfield_for_foreignkey(db_field, request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request, using=self.using, **kwargs)
class Id3WebRouterMixin(object):
"""
A router to control all database operations on models in the
id3web application.
"""
list_id3_applications = [
'clients',
'licences',
]
def db_for_read(self, model, **hints):
"""
Attempts to read licences models go to db_id3web.
"""
if model._meta.app_label in Id3WebRouterMixin.list_id3_applications:
return 'db_id3web'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write licences models go to db_id3web.
"""
if model._meta.app_label in Id3WebRouterMixin.list_id3_applications:
return 'db_id3web'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the licences app is involved.
"""
if ((obj1._meta.app_label in Id3WebRouterMixin.list_id3_applications) or
(obj2._meta.app_label in Id3WebRouterMixin.list_id3_applications)):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the licences app only appears in the 'db_id3web'
database.
"""
if app_label in Id3WebRouterMixin.list_id3_applications:
return db == 'db_id3web'
return None
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# clients.routers.py
# http://python-future.org/imports.html
# To write a Python 2/3 compatible codebase, the first step is to add this line
# to the top of each module:
from common.db_id3web import Id3WebRouterMixin
class ClientRouter(Id3WebRouterMixin):
"""
A router to control all database operations on models in the
licences application.
"""
pass
# settings.base.py
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
# https://docs.djangoproject.com/en/dev/topics/db/multi-db/
# https://github.com/joke2k/django-environ
#================================================================
DATABASES = {
'default': env.db('DATABASE_DJANGO'),
'db_id3web': env.db('DATABASE_MASTER_ID3WEB')
}
# https://docs.djangoproject.com/en/dev/topics/db/multi-db/#topics-db-multi-db-routing
DATABASE_ROUTERS = [
'clients.routers.ClientRouter',
'licences.routers.LicenceRouter',
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment