Commit c8b3d10c authored by lixiaofang's avatar lixiaofang

add DBRouter

parent b15661d2
# !/usr/bin/env python
# -*- coding: utf-8 -*-
class DemoRouter:
"""
A router to control all database operations on models in the
user application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read user models go to users_db.
"""
if model._meta.app_label == 'face':
return 'face'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write user models go to users_db.
"""
if model._meta.app_label == 'face':
return 'face'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the user app is involved.
"""
if obj1._meta.app_label == 'face' or \
obj2._meta.app_label == 'face':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'users_db'
database.
"""
if app_label == 'face':
return db == 'face'
return None
......@@ -123,6 +123,7 @@ REDIS_URL = "redis://127.0.0.1:6379"
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASE_ROUTERS = ['./DBRouter']
DATABASE_APPS_MAPPING = {'face': 'face'}
DATABASES = {
......
......@@ -10,11 +10,12 @@ import datetime
from django.db.models import QuerySet
from .topic import Topic
manager = lambda: models.Manager().db_manager(using='face')
# manager = lambda: models.Manager().db_manager(using='face')
class ContrastSimilar(models.Model):
class Meta:
app_label = 'face'
verbose_name = u"用户脸相似度表"
db_table = "face_user_contrast_similar"
......@@ -25,7 +26,7 @@ class ContrastSimilar(models.Model):
# return qs
# objects = models.Manager().db_manager(using='face')
objects = manager()
# objects = manager()
id = models.IntegerField(verbose_name="主键ID", primary_key=True)
is_online = models.BooleanField(verbose_name="是否上线")
is_deleted = models.BooleanField(verbose_name="是否删除")
......
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