Commit 3f56b842 authored by lixiaofang's avatar lixiaofang

add

parent 6f22f345
# !/usr/bin/env python # !/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
DATABASE_APPS_MAPPING = {'face': 'face', 'commodity': 'commodity'}
class DBRouter: class DBRouter:
...@@ -15,8 +16,8 @@ class DBRouter: ...@@ -15,8 +16,8 @@ class DBRouter:
# if model._meta.app_label == 'face': # if model._meta.app_label == 'face':
# return 'face' # return 'face'
if model._meta.app_label == 'commodity': if model._meta.app_label in DATABASE_APPS_MAPPING:
return 'commodity' return DATABASE_APPS_MAPPING[model._meta.app_label]
return None return None
def db_for_write(self, model, **hints): def db_for_write(self, model, **hints):
...@@ -26,29 +27,32 @@ class DBRouter: ...@@ -26,29 +27,32 @@ class DBRouter:
# if model._meta.app_label == 'face': # if model._meta.app_label == 'face':
# return 'face' # return 'face'
if model._meta.app_label == 'commodity': if model._meta.app_label in DATABASE_APPS_MAPPING:
return 'commodity' return DATABASE_APPS_MAPPING[model._meta.app_label]
return None return None
def allow_relation(self, obj1, obj2, **hints): def allow_relation(self, obj1, obj2, **hints):
""" """
Allow relations if a model in the user app is involved. Allow relations if a model in the user app is involved.
# """ # """
# if obj1._meta.app_label == 'face' or \ db_obj1 = DATABASE_APPS_MAPPING.get(obj1._meta.app_label)
# obj2._meta.app_label == 'face': db_obj2 = DATABASE_APPS_MAPPING.get(obj2._meta.app_label)
# return True if db_obj1 and db_obj2:
if obj1._meta.app_label == 'commodity' or \ if db_obj1 == db_obj2:
obj2._meta.app_label == 'commodity': return True
return True else:
return None return False
else:
return None
def allow_migrate(self, db, app_label, model_name=None, **hints): def allow_migrate(self, db, app_label, model_name=None, **hints):
""" """
Make sure the auth app only appears in the 'users_db' Make sure the auth app only appears in the 'users_db'
database. database.
""" """
# if app_label == 'face': if db in DATABASE_APPS_MAPPING.values():
# return db == 'face' return DATABASE_APPS_MAPPING.get(app_label) == db
if app_label == 'commodity': elif app_label in DATABASE_APPS_MAPPING:
return db == 'commodity' return False
return None return None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment