Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1.  
  2. class DBRouter(object):
  3.     """
  4.    Docs: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example
  5.    A router to control all database operations on models in the
  6.    auth application.
  7.  
  8.    $ ./manage.py migrate
  9.    $ ./manage.py migrate --database=auth_db
  10.    """
  11.  
  12.     def db_for_read(self, model, **hints):
  13.         """
  14.        Attempts to read auth models go to auth_db.
  15.        """
  16.         if model._meta.app_label == 'auth':
  17.             return 'auth_db'
  18.         return 'default'
  19.  
  20.     def db_for_write(self, model, **hints):
  21.         """
  22.        Attempts to write auth models go to auth_db.
  23.        """
  24.         if model._meta.app_label == 'auth':
  25.             return 'auth_db'
  26.         return 'default'
  27.  
  28.     def allow_relation(self, obj1, obj2, **hints):
  29.         """
  30.        Allow relations if a model in the auth app is involved.
  31.        """
  32.         if obj1._meta.app_label == 'auth' or \
  33.            obj2._meta.app_label == 'auth':
  34.             return True
  35.         return 'default'
  36.  
  37.     def allow_migrate(self, db, app_label, model_name=None, **hints):
  38.         """
  39.        Make sure the auth app only appears in the 'auth_db'
  40.        database.
  41.        """
  42.         if app_label == 'auth':
  43.             return db == 'auth_db'
  44.         return 'default'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement