Django - 多个数据库和auth.Permission [英] Django - multiple databases and auth.Permission

查看:448
本文介绍了Django - 多个数据库和auth.Permission的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要两个数据库的项目 - 一个用于注销部分,另一个用于登录。我需要同时处理这两个数据库的auth(因此contenttypes)应用程序,这是正常的。但是,用于创建缺省Permission和ContentType对象的auth和contenttypes的管理命令不会在登录数据库中运行,而仅在默认的情况下运行。我有这个权利吗?



我的数据库路由器



  LOGGED_IN_APPS =('头像' 
COMMON_APPS =('auth','contenttypes','注册')

class MyRouter(object )
def db_for_read(self,model,** hints):
如果LOGGED_IN_APPS中的model._meta.app_label为
返回'logged_in'
返回无

def db_for_read(self,model,** hints):
如果LOGGED_IN_APPS中的model._meta.app_label为
返回'logged_in'
返回无

def allow_relation(self,obj1,obj2,** hints):
如果LOGGED_IN_APPS中的obj1._meta.app_label或LOGGED_IN_APPS中的obj2._meta.app_label:
返回True
返回无

def allow_syncdb(self,db,model):
如果db =='logged_in':
返回model._meta.app_label在LOGGED_IN_APPS或model._m et_app_label in COMMON_APPS
elif model._meta.app_label in LOGGED_IN_APPS:
return False
return None


解决方案

这是我做的。首先,没有办法告诉syncdb在特定数据库上创建权限 - 它总是选择默认值。所以,由于这个项目的性质,我能把它分成两个项目,每一个都有自己的数据库。这解决了我的问题,但不幸的是Django需要修补以支持在多个数据库上执行此操作。


I'm working on a project that needs two databases - one for the "logged out" portion and one for the logged in. I need the auth (and therefore contenttypes) app synched to both databases, which is working fine. However, the management commands for auth and contenttypes that create the default Permission and ContentType objects aren't running on the logged in database, only the default one. Do I have this right?

My database router

LOGGED_IN_APPS = ('avatar', 'guardian', 'money', 'ipn', 'schedule', 'studio')
COMMON_APPS = ('auth', 'contenttypes', 'registration')

class MyRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label in LOGGED_IN_APPS:
            return 'logged_in'
        return None

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

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

    def allow_syncdb(self, db, model):
        if db == 'logged_in':
            return model._meta.app_label in LOGGED_IN_APPS or model._meta.app_label in COMMON_APPS
        elif model._meta.app_label in LOGGED_IN_APPS:
            return False
        return None

解决方案

Here's what I did. First, there is no way to tell syncdb to create permissions on a specific database - it always will pick the default. So, because of the nature of this project, I was able to split it into two projects, each with their own database. This solves the problem for me, but unfortunately Django would need to be patched to support doing this on multiple databases.

这篇关于Django - 多个数据库和auth.Permission的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆