Django - multiple databases and auth.Permission

2019-07-15 11:46发布

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

1条回答
不美不萌又怎样
2楼-- · 2019-07-15 11:57

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.

查看更多
登录 后发表回答