Django - 多个数据库和 auth.Permission

发布于 2024-09-14 06:10:34 字数 1124 浏览 4 评论 0原文

我正在开发一个需要两个数据库的项目 - 一个用于“注销”部分,一个用于登录。我需要将身份验证(以及内容类型)应用程序同步到两个数据库,这工作正常。但是,创建默认 Permission 和 ContentType 对象的 auth 和 contenttypes 管理命令不在登录数据库上运行,仅在默认数据库上运行。我有这个权利吗?

我的数据库路由器

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-09-21 06:10:34

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文