Django - multiple databases and auth.Permission

六眼飞鱼酱① 提交于 2019-12-12 19:09:36

问题


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:


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.



来源:https://stackoverflow.com/questions/3469914/django-multiple-databases-and-auth-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!